0

I am trying to upload image with multiple parameters using alamofire multipart with swift 4 but I can not upload image sucessfully and I got response like

{
  "error" : "error uploading file, please retry"
}

Here is my function which I call on upload button event.

// selectedLogo = (info["UIImagePickerControllerOriginalImage"] as? UIImage)!
let imageData = UIImageJPEGRepresentation(selectedLogo, 1.0)
let parameters: Parameters = ["id": strUserId,
                              "telephone": self.txtTelephoneNumber.text!,
                              "email": self.txtEmail.text!,
                              "notice":self.txtNotices.text!,
                              "status" : userData["status"]]

print(parameters)

Alamofire.upload(multipartFormData: { (multipartFormData) in
    if let data = imageData{
        multipartFormData.append(data, withName: "club_image", fileName: "file.jpg", mimeType: "image/jpg")
    }
    for (key, value) in parameters {
        multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!,withName: key as String)

    }
}, to:"\(self.app.strBaseAPI)updatedata.php")
{ (result) in
    switch result {
    case .success(let upload, _, _):
        upload.uploadProgress(closure: { (progress) in
            //Print progress
            print(progress)
        })
        upload.validate()

        upload.responseJSON { response in
            if response.response?.statusCode == 200
            {
                if response.result.isSuccess == true
                {
                    if let value = response.result.value
                    {
                        self.json = JSON(value)
                        print(self.json)

                        let strStatus : String = self.json["success"].stringValue
                        if strStatus == "true"
                        {
                            Toast(text: strStatus).show()
                        }else{
                            Toast(text: strStatus).show()
                        }
                    }else{
                        Toast(text: "Something wrong").show()
                    }
                }else{
                    Toast(text: "Something wrong").show()
                }
            }else{
                SVProgressHUD.dismiss()
                Toast(text: "Something wrong").show()
            }
        }

    case .failure(let encodingError):
        //print encodingError.description
        print(encodingError.localizedDescription)
    }

when I convert image using UIImagePNGRepresentation with same method just change one line

multipartFormData.append(imageData!, withName: "image", fileName: "image.png", mimeType: "image/png")

it will give me like this

SUCCESS: {
    error = "error data post";
}

Please help me!!

riddhi
  • 316
  • 2
  • 16
  • Possible duplicate of [Alamofire 4 upload with parameters](https://stackoverflow.com/questions/39809867/alamofire-4-upload-with-parameters) – GIJOW Mar 02 '18 at 12:13
  • @GIJOW I try that solution also but I can't help me. – riddhi Mar 02 '18 at 17:09

2 Answers2

0

There is no such MIME Type as image/jpg. Change it to image/jpeg. enter image description here

Aleksandr Honcharov
  • 2,343
  • 17
  • 30
0

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. Try to updated the compression for the JPEG images from

if  let imageData = UIImageJPEGRepresentation(selectedLogo, 1)

to

if  let imageData = UIImageJPEGRepresentation(selectedLogo, 0.6)

Hope this will help you.

Rashed
  • 2,349
  • 11
  • 26
  • I change compress value but can't upload image – riddhi Mar 02 '18 at 17:08
  • Yes same response get success status code(200) but error same occur. – riddhi Mar 02 '18 at 17:13
  • That should work. You can see this solutions to solve this issue - https://stackoverflow.com/questions/43972651/multiple-file-upload-with-array-of-parameters-using-alamofire – Rashed Mar 02 '18 at 17:22