SwiftStudier
SwiftStudier

Reputation: 2324

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?

Upvotes: 4

Views: 7317

Answers (2)

cnoon
cnoon

Reputation: 16663

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.

Upvotes: 6

Thellimist
Thellimist

Reputation: 4027

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.

Upvotes: 1

Related Questions