0

I want to upload some file to a server, but it needs a token to post with the file together. I got the token when I logged in, so how could I post to the server? Can I write code like this?

var par=[
           "token":"xxxxxxxxxx",
           "file":"filename.file"
]
Alamofire.upload(.POST, "http://www.xxxxx.xxx", parameters: par)
John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

0

This is most likely not supported in the current version of Alamofire depending on your server implementation.

Multipart Form Data

Your server most likely expects the data to be multipart/form-data encoded. Currently, multipart form data is not supported by Alamofire. You will need to encode the data yourself according to the RFC-2388 and RFC-2045.

If this ends up being the case, you can either implement your own version of the specs, or you could use AFNetworking. I would encourage you at the moment to use AFNetworking if this is the case. Here is a thread (courtesy of @rainypixels) to get you started if you decide you really want to implement this yourself.

You need to be careful with this option as it is an in-memory solution. Do NOT attempt to upload videos or large numbers of images in this way or your app out-of-memory very quickly.

File Upload

If the server does not expect multipart/form-data encoding, then you can use the Alamofire upload method.

public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request

You could create an NSURLRequest with the token appended as a parameter, then pass the fileURL off to Alamofire to be uploaded.

In summary, I'm pretty certain that the first approach is what your server is going to require. Either way, hopefully this helps you get heading in the right direction.

Community
  • 1
  • 1
cnoon
  • 16,575
  • 7
  • 58
  • 66
  • Is there a reason you didn't recommend rolling a custom multipart implementation in conjunction with Alamofire.upload, @cnoon? Something like this, for instance — http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire/26747857#26747857 (there are some more robust multipart encoding implementations out there, too). It'd be a pretty hard-coded solution, but would get the job done with 15-20 lines of code. Just wondering if I'm missing something. – rainypixels Mar 24 '15 at 07:08
  • You make a really good point. I've updated my answer accordingly with your link and a warning about in-memory usage. The main reason I don't encourage people to attempt to build this on their own is that there are many different things to consider that can bite you. I've already built a robust solution which I am planning on working into an Alamofire PR here soon. – cnoon Mar 24 '15 at 15:36
  • Makes sense. And yes, I was very apprehensive about rolling my own. I've been keeping up on the threads on github, so I figured that it'd be a matter of time before someone would pull request this feature. In the meantime, I have a simple placeholder that does the job for my app which is still a ways to production. It'll be easier to rip out my code whenever your PR is merged. Very glad to hear that you've taken that on, btw. I'll stay tuned, and thanks for contributing! :) – rainypixels Mar 24 '15 at 16:50
  • Hi @Learningpro, if this answer is sufficient could you upvote and mark as such? – cnoon Apr 06 '15 at 15:31