0

I am using URLSession 'uploadTask from file'

func uploadTask(with request: URLRequest, fromFile fileURL: URL) -> URLSessionUploadTask

Almost everything works fine, but now our server needs an extra param as 'uploadKey' to be passed as content disposition along with fileName.

This can be done by generating multipart request with content disposition added as we normally do.

I want to add it while using 'uploadTask from file' to avoid memory pressure. Please suggest how to do it.

infiniteLoop
  • 2,135
  • 1
  • 25
  • 29

1 Answers1

0

From reading the question, I suspect that you're subtly misunderstanding what upload tasks do (and unfortunately, Apple's documentation needs some serious improvement in this area, which doesn't help matters). These tasks don't upload a file in the same way that a web browser would if you chose a file in an upload form. Rather, they use the file as the body of an upload request. I think they default to providing a sane Content-Type based on the filename, though I'm not certain, but they do not send data in form encoding.

So assuming I'm fully understanding the question, your options are either:

  1. Keep using multipart encoding. Optionally write the multipart body into a file rather than keeping it in memory, and use the upload task to provide the body from that file instead of from an NSData object.
  2. Upload the file you're trying to send, unencoded, as the entirety of the upload body, and provide whatever additional parameters you need to provide in the form of GET parameters in the URL itself.
  3. Use some other encoding like JSON or protocol buffers.

Either way, the server code will determine which of these approaches is supported. If you can modify the server code, then I would recommend the second approach. It is slightly more efficient than the first approach, much more efficient than JSON, and easier to implement than any of the other approaches.

dgatwood
  • 10,129
  • 1
  • 28
  • 49