15

I'm doing the below to upload a PNG file with parameters:

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(UIImagePNGRepresentation(tempImage!)!, withName: "file", fileName: "picture.png", mimeType: "image/png")

            // Send parameters
            multipartFormData.append((UserDefaults.standard.value(forKey: Email) as! String).data(using: .utf8)!, withName: "email")
            multipartFormData.append("png".data(using: .utf8)!, withName: "type")

        },
        to: "user/picture",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint("SUCCESS RESPONSE: \(response)")
                }
            case .failure(let encodingError):
                self.removeSpinnerFromView()
                print("ERROR RESPONSE: \(encodingError)")

            }
        }
    )

Problem is that on my server I don't see the email and type form fields. I followed examples posted online for this. Is there anything I should do differently for this?

EDIT

If I remove the part where I put:

multipartFormData.append(UIImagePNGRepresentation(tempImage!)!, withName: "file", fileName: "picture.png", mimeType: "image/png")

THEN the parameters are included. Otherwise not, I think this is a bug in Alamofire 4.0.1.

KVISH
  • 12,923
  • 17
  • 86
  • 162

2 Answers2

44

Its working fine on my side.

I'm using following code:

let parameters = [
            "file_name": "swift_file.jpeg"
        ]

Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 1)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
            }, to:"http://sample.com/upload_img.php")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
            })

            upload.responseJSON { response in
                //print response.result
            }

        case .failure(let encodingError):
               //print encodingError.description
        }
    }
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
  • in for (key, value) in parameters , is Value type 'AnyObject'? – Cruz Oct 03 '16 at 15:21
  • @MicroR Its working fine on my side. It might be backend issue. – Ekta Padaliya Apr 03 '17 at 05:59
  • @EktaPadaliya. Can u guide how to upload image using swift 3 alamofire 4. I have done for swift 2.3 but not able achieve with swift 3. Please guide me. – Uma Madhavi Apr 07 '17 at 12:35
  • @EktaPadaliya. No , but i need to include headers also how can i add – Uma Madhavi Apr 07 '17 at 12:40
  • @UmaMadhavi you have to add "import Alamofire" . – Ekta Padaliya Apr 07 '17 at 12:44
  • @EktaPadaliya. I have imported . please check my code in swift 2.3 .This is my code http://pasted.co/0d44560e password 12345 – Uma Madhavi Apr 07 '17 at 12:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/140178/discussion-between-uma-madhavi-and-ekta-padaliya). – Uma Madhavi Apr 07 '17 at 12:51
  • @EktaPadaliya how to convert back the jpeg representation to image format or How to check the multipart received at the server end has received the proper image. – amish Mar 21 '18 at 07:20
  • @amish You can check on REST API that "photo_path" this key exist or not. – Ekta Padaliya Mar 21 '18 at 09:31
  • @EktaPadaliya my receiving end of alamofire response is on visual studio. So when I print the alamofire multpart body. I get the data and jpeg image. I have no idea what to do next to get the image back. I just have some bytes of the Image. You can refer to this question [https://stackoverflow.com/questions/49237751/alamofire-encodes-image-in-which-format] – amish Mar 21 '18 at 09:36
2

If your value is of type Any, you can change it like this

for (key, value) in params {
    let paramsData:Data = NSKeyedArchiver.archivedData(withRootObject: value)
    formData.append(paramsData, withName: key)
}
Antti29
  • 2,953
  • 12
  • 34
  • 36
Yun
  • 41
  • 4