open func uploadFile(fileName : String, contentType:String, fileData:Data?, parameters:Dictionary,headerParams:Dictionary, completionHandler:@escaping (Dictionary) -> (Void)){
var request = URLRequest(url: URL(string: A2Config.getBaseURL() + "/uploadFile")!)
request.httpMethod = "POST"
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
for (key,valueForKey) in headerParams
{
request.setValue(valueForKey, forHTTPHeaderField: key)
}
let body = NSMutableData()
let boundaryPrefix = "--\(boundary)\r\n"
for (key, value) in parameters {
body.appendString(boundaryPrefix)
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
body.appendString(boundaryPrefix)
body.appendString("Content-Disposition: form-data; name=\"uploadFile\"; filename=\"\(fileName)\"\r\n")
body.appendString("Content-Type: \(contentType)\r\n\r\n")
body.append(fileData!)
body.appendString("\r\n")
body.appendString("--".appending(boundary.appending("--")))
request.httpBody = body as Data
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
completionHandler(["status" : "error","errMsg":"Error completing request, please re-login status :\(httpStatus)"])
return
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
do
{
let jsonData : Dictionary? = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? Dictionary
guard jsonData != nil else {
completionHandler(["status" : "error"])
return
}
completionHandler(["status" : "success","data":jsonData!])
}
catch let err {
print("met with an error \(err)")
completionHandler(["status" : "error"])
}
}
task.resume()
}
var request = URLRequest(url: URL(string: A2Config.getBaseURL() + "/uploadFile")!)
request.httpMethod = "POST"
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
for (key,valueForKey) in headerParams
{
request.setValue(valueForKey, forHTTPHeaderField: key)
}
let body = NSMutableData()
let boundaryPrefix = "--\(boundary)\r\n"
for (key, value) in parameters {
body.appendString(boundaryPrefix)
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
body.appendString(boundaryPrefix)
body.appendString("Content-Disposition: form-data; name=\"uploadFile\"; filename=\"\(fileName)\"\r\n")
body.appendString("Content-Type: \(contentType)\r\n\r\n")
body.append(fileData!)
body.appendString("\r\n")
body.appendString("--".appending(boundary.appending("--")))
request.httpBody = body as Data
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
completionHandler(["status" : "error","errMsg":"Error completing request, please re-login status :\(httpStatus)"])
return
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
do
{
let jsonData : Dictionary
guard jsonData != nil else {
completionHandler(["status" : "error"])
return
}
completionHandler(["status" : "success","data":jsonData!])
}
catch let err {
print("met with an error \(err)")
completionHandler(["status" : "error"])
}
}
task.resume()
}
No comments:
Post a Comment