Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multipart form-data file uploading using Alamofire

I have looked at questions like this or that one but it is still not working. enter image description here

Also I have a question about what should I enter into fileURL param from function multipartFormData.appendBodyPart?

Should it be a way to image from PC, or image must be added to Images.xcassets? What should I send here?

like image 373
SwiftStudier Avatar asked Mar 16 '23 05:03

SwiftStudier


2 Answers

It looks like you have three issues that you need to fix.

  1. Use .POST instead of POST.
  2. The fileURL needs to be a valid NSURL that points to a file on the file system. You cannot just use the filename.
  3. You are using the responseString serializer, but named the third parameter in the closure JSON. Then you are letting result into s and trying to print it out. The result parameter doesn't even exist anywhere. Instead, you should print(JSON).

Hopefully that helps clear things up a bit.

like image 95
cnoon Avatar answered Apr 01 '23 18:04

cnoon


Try to use .POST not POST


As an alternative solution upload an encoded file and send it as a parameter of POST.

// `data` is NSData
let base64String = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)

let parameters = ["image_data": base64String] as [String: AnyObject]
Alamofire.request(.POST, "http://your-url.com", parameters: parameters)

The cons of this method is that the data will get %33 larger due to encoding. If you have bandwidth problems it may not be a good solution.

like image 30
Thellimist Avatar answered Apr 01 '23 18:04

Thellimist