2

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)
YakovL
  • 7,557
  • 12
  • 62
  • 102
Vaisakh
  • 2,919
  • 4
  • 26
  • 44
  • Possible duplicate of [Uploading file with parameters using Alamofire](http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire) – Eric Aya Oct 15 '15 at 15:33
  • 1
    its not the duplicate i need to pass an Array of data as parameter but when i try this multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key as! String) app is crashing – Vaisakh Oct 16 '15 at 04:16

2 Answers2

1

I’ve had this problem when I try to upload an image (Alamofire) with parameters like that(dictionary within dictionary).

Here's an example:

let params:[String:Any] = ["client" : ["name" : "Some", "email" : "some@some.com"]]
for (key, value) in (params["client"] as! [String:Any]) {
     multipartFormData.append((value as! String).data(using: String.Encoding.utf8)!, withName: "client[\(key)]")
 }

The trick is in this part:

withName: "client[\(key)]"
FelipeCN
  • 11
  • 1
0

change your code from

for (key, value) in dictionary {
    multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key as! String)
}

to this :

for (key, value) in parameters {
    multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}

This works for me.

YakovL
  • 7,557
  • 12
  • 62
  • 102
youngeun
  • 86
  • 5
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – mech Feb 23 '16 at 19:09