I am using Alamofire
for sending request to the server. Now I have to send an array of images(Multiple images) to the server with other parameters. Maximum 4 images I have to send. Please someone helps me, how to solve this task. I checked StackOverflow for a solution but all solutions are like sending the single image to the server. But I want to send multiple images using Alamofire
because I have array of images.
This is my code
func clockOutFor(userId: NSNumber, projectId: NSNumber, taskId: NSNumber, latitude: CLLocationDegrees, longitude: CLLocationDegrees, deviceClockOutTime: String, actualEndTime: String, clockOutNetworkInfo: String, clockOutBatteryStatus: String, totalDistance: NSNumber, durationTime: String, customeLabel1: String, customeLabel2: String, customeLabel3: String, customeLabel4: String, customeLabel5: String, imageDataArray: NSArray, completionHandler: @escaping CompletionBlock ) -> Void
{
let parameter : Parameters = ["gs_userId":userId, "gs_taskId":taskId, "gs_project_id":projectId, "gs_actual_end":actualEndTime, "gs_actual_end_lattitude":latitude, "gs_actual_end_longitude":longitude, "gs_clockout_device_time":deviceClockOutTime, "gs_clockout_network_status":clockOutNetworkInfo, "gs_clockout_battery_status":clockOutBatteryStatus, "gs_distance":totalDistance, "gs_time_taken":durationTime, "gs_custom1_label":customeLabel1, "gs_custom1_labe2":customeLabel2, "gs_custom1_labe3":customeLabel3, "gs_custom1_labe4":customeLabel4, "gs_custom1_labe5":customeLabel5] as [String : AnyObject]
let url = "clockout-update"
let fullUrl = baseUrl?.appendingPathComponent(url)
let headers: HTTPHeaders = [
"Authorization" : "Bearer \(token!)",
"Accept": "application/json",
"Connection": "keep-alive",
"Content-type": "multipart/form-data"
]
if token != nil {
Alamofire.upload(multipartFormData: { multipartFormData in
for i in 0..<imageDataArray.count{
let imageData1 = UIImageJPEGRepresentation(imageDataArray[i] as! UIImage, 1.0)!
multipartFormData.append(imageData1, withName: "morephoto[\(i)]" , fileName: "photo" + String(i) + ".jpg", mimeType: "image/jpeg")
}
for (key, value) in parameter {
print("Key and Value = ",key, value)
if let data = (value as AnyObject).data(using: String.Encoding.utf8.rawValue) {
multipartFormData.append(data, withName: key)
}
}
},
to: fullUrl!,method:HTTPMethod.post,
headers:headers, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload
.validate()
.responseJSON { response in
print(response.request as Any) // original URL request
print(response.response as Any) // URL response
print(response.data as Any) // server data
print("Result",response.result) // result of response serialization
print("parameters = \(parameter)")
switch response.result {
case .success(let value):
completionHandler(value as AnyObject, "No error found")
print("responseObject: \(value)")
case .failure(let responseError):
print("responseError: \(responseError)")
}
}
case .failure(let encodingError):
print("encodingError: \(encodingError)")
let errorDesc = (encodingError as NSError).localizedDescription
completionHandler(errorDesc as NSString,"Some error found")
}
})
}
}
Here I am getting one error in this line
if let data = (value as AnyObject).data(using: String.Encoding.utf8.rawValue)
I think it is expecting only string parameters but I have both String and NSNumber. So here is my question, How to encode both values string and NSNumber. Plese someone help/advise me.