I am working on multi part file upload with Alamofire Library in Swift. I need to pass some parameters along with the file. The following is the body I need to pass to the server:
{
"eventId":7,
"eventImageList":[
{ "contentType":"png", "imageName":"test", "imagePath":"" }
,
{ "contentType":"png", "imageName":"test2", "imagePath":"" }
]
}
I have tried similar to this:
let image = UIImage(named: "sampleImage")
Alamofire.upload(.POST, URL, multipartFormData: {
multipartFormData in
if let _image = image {
if let imageData = UIImageJPEGRepresentation(_image, 0.5) {
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.png", mimeType: "image/png")
}
}
for (key, value) in dictionary {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key as! String)
}
}, encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .Success:
print("jsonResponse ==== ", response)
case .Failure(let error):
print("error ==== ", error)
}
}
case .Failure(let encodingError):
print(encodingError)
}
})
I need to pass this array:
[
{ "contentType":"png", "imageName":"test", "imagePath":"" }
,
{ "contentType":"png", "imageName":"test2", "imagePath":"" }
]
but when I decode it like below, the app crashes.
multipartFormData.appendBodyPart(data:value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)