Swift 3 Alamofire 4 Upload array of images with parameters

佐手、 提交于 2019-12-11 02:37:19

问题


I'm trying to upload an array of pictures along with some parameters using Alamofire 4 and Swift 3.

The parameters seem to work because the update is done, but the images don't get to the server

In Postman I can do this fine with no problem:

Postman request sample

This is my code so far:

    let parameters = [
        "service_request_id" : servicesID,
        "status_id" : "4",
    ]
    Alamofire.upload(
        multipartFormData: { multipartFormData in

            for (key,value) in parameters {
                if let value = value as? String {
                    multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                }
            }

            for (image) in self.imagesArray {
                if  let imageData = UIImageJPEGRepresentation(image, 1) {
                    multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
                }
            }
    },
        to: ConnectionWS.UpdateServicesURL,
        method: .put,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    print(progress)
                })

                upload.responseJSON { response in

                    // If the request to get activities is succesfull, store them
                    if response.result.isSuccess{
                        print(response.debugDescription)
                        alert.dismiss(animated: true, completion:
                            {
                                self.dismiss(animated: true, completion:
                                    {
                                        self.delegate?.statusChanged(IsFinish: false)
                                })

                        })

                        // Else throw an error
                    } else {


                        var errorMessage = "ERROR MESSAGE: "
                        if let data = response.data {
                            // Print message
                            let responseJSON = JSON(data: data)
                            if let message: String = responseJSON["error"]["message"].string {
                                if !message.isEmpty {
                                    errorMessage += message
                                }
                            }
                        }
                        print(errorMessage) //Contains General error message or specific.
                        print(response.debugDescription)
                    }

                    alert.dismiss(animated: true, completion:
                        {
                            self.dismiss(animated: true, completion:nil)

                    })
                }
            case .failure(let encodingError):
                print("FALLE ------------")
                print(encodingError)
            }
    }
    )

Am I doing something wrong?

Please help.


回答1:


So the error was from the server side, that had a very small size limit for images and that's why they weren't saving. I updated the compression for the JPEG images from

if  let imageData = UIImageJPEGRepresentation(image, 1)

to

if  let imageData = UIImageJPEGRepresentation(image, 0.6)

and now it's working fine.

Thank you @Sneak for your links.




回答2:


for (key,value) in parameters {
                if let value = value as? String {
                    multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                }
            }

            for (image) in self.imagesArray {
                if  let imageData = UIImageJPEGRepresentation(image, 1) {
                    multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
                }
            }

//change this code to below 

for (key,value) in parameters {
                if let value = value as? String {
                    if value == "image" {
                        multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
                    } else {
                        multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)

                    }
                }
            }


来源:https://stackoverflow.com/questions/42427541/swift-3-alamofire-4-upload-array-of-images-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!