4

I'm trying to upload an image with Alamofire, however it does not seem to upload.

 Alamofire.upload(multipartFormData: { multipartFormData in multipartFormData.append(imgData, withName: "data", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpg") multipartFormData.append(operaID.data, withName: "id") }, to: URL_CORDINATE) { (result) in switch result { case .success(let upload, _, _): upload.uploadProgress(closure: { (progress) in print("Upload Progress: \(progress.fractionCompleted)") }) upload.responseJSON { response in print("Upload image response:", response.result.value) } case .failure(let encodingError): print("Error while uploading image:", encodingError) } 

I'm expecting to see the image upload, however there seems to be something wrong with the body (I should pass the img and id in the body not as parameters!)

It does however work in Postman: https://i.sstatic.net/f0mUd.png

2
  • Is there any error output? Commented May 1, 2019 at 14:42
  • No error in the output Commented May 1, 2019 at 14:44

3 Answers 3

1

you can try this:

Alamofire.upload( multipartFormData: { multipartFormData in multipartFormData.append(imgData!, withName: "img", fileName: "jpg", mimeType: "image/jpeg") }, to: UPLOAD_IMAGE_URL, encodingCompletion: { (encodingResult) -> Void in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in let result = response.data do { print("success") }catch { print("error") } } case .failure(let encodingError): print(encodingError) } }) 
Sign up to request clarification or add additional context in comments.

Comments

0

For the first look it's missing the HTTP method: .post, and the mimeType should be "image/jpeg", not "image/jpg".

See this:

 Alamofire.upload(multipartFormData: { multipartFormData in multipartFormData.append(imgData, withName: "data", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg") multipartFormData.append(operaID.data, withName: "id") }, to: YOUR_URL_HERE, method: .post, encodingCompletion: { result in switch result { case .success(let upload, _, _): upload.uploadProgress(closure: { (progress) in print("Upload Progress: \(progress.fractionCompleted)") }) upload.responseJSON { response in print("Upload image response:", response.result.value) } case .failure(let encodingError): print("Error while uploading image:", encodingError) } }) 

Is there any server log indicating error? Can you add breakpoints to the server code and run it on localhost to check what you have in your controller?

In case the server endpoint is http put this in your Info.plist:

<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 

5 Comments

I've tried making the changes you suggested, however it still seems not to be working (when I print the result given by Alamofire I get: "CredStore - performQuery - Error copying matching creds"). Not sure why!
What is your url? Is it http or https? This error can occur with http server endpoints.
artaugmentedreality.com/api/postpreview with "img" (image) and "id" (e.g. 44) in the body.
I added the domain to the list in info.plist of supported HTTP domains, but I still get the same error! :(
How is your Info.plist look like? Extended my answer.
0
 Alamofire.upload(multipartFormData: { multipartFormData in if let img = image, let imageData = img.jpegData(compressionQuality: 0.6) { multipartFormData.append(imageData, withName: imageName, fileName: "temp.jpg", mimeType: "image/jpg") } for (key, value) in parameter ?? [:] { if value is String || value is Int { multipartFormData.append("\(value)".data(using: .utf8)!, withName: key) } } }, to: url, headers: headers, encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _ , _ ): upload.responseJSON { response in debugPrint(response) if response.response?.statusCode == 200 { if let jsonData = try? JSONSerialization.data(withJSONObject: response.result.value!, options: .prettyPrinted) { print("Response: \n",String(data: jsonData, encoding: String.Encoding.utf8) ?? "nil") } success(response.result.value! as AnyObject) }else { let res = response.result.value! as AnyObject let msg = res["Message"] as? String if msg != nil { failure(msg!) }else { failure("") } } } case .failure(let encodingError): print(encodingError) failure(encodingError.localizedDescription) } }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.