3
votes

I need to upload an mp4 video file from iPhone/iPad to a server, also in the background, so I read that is possible with URLSession.uploadTask(with: URLRequest, fromFile: URL) method, but I don't understand how do I prepare the request before.I need to create a multipart/form-data request because I want to append other string parameters.

func requestBodyFor(video: URL) -> Data? {
    let url = URL(string: "url_of_upload_handler.php")!

    let parameters = ["type":"video", "user":"112"]

    do {

        let kBoundary = "Boundary-\(UUID().uuidString)"
        let kStartTag = "--%@\r\n"
        let kEndTag = "\r\n"
        let kContent = "Content-Disposition: form-data; name=\"%@\"\r\n\r\n"

        var body = Data()

        let videoData = try Data(contentsOf: video)

        // parameters
        for (key,value) in parameters {
            body.append(String(format: kStartTag, kBoundary).data(using: String.Encoding.utf8)!)
            body.append(String(format: kContent, key).data(using: String.Encoding.utf8)!)
            body.append(value.data(using: String.Encoding.utf8)!)
            body.append(String(format: kEndTag).data(using: String.Encoding.utf8)!)
        }

        //Video data
        body.append(String(format: kStartTag, boundary).data(using: String.Encoding.utf8)!)
        body.append(String(format: "Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", "file", video.lastPathComponent).data(using: String.Encoding.utf8)!)
        body.append("Content-Type: video/mp4\r\n\r\n".data(using: String.Encoding.utf8)!)
        body.append(videoData)
        body.append(String(format: kEndTag).data(using: String.Encoding.utf8)!)

        // close form
        body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

       return body
    } catch let error {
        print(error)
        return nil
    }
}


if let body = requestBodyFor(video: fileUrl) {
        let contentType = "multipart/form-data; boundary=\(kBoundary)"

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue(contentType, forHTTPHeaderField: "Content-Type")

        let task = URLSession.shared.uploadTask(with: request, from: body) { data, response, error in

        guard error == nil && data != nil else {
          return
        }

        if let data = String(data: data!, encoding: String.Encoding.utf8) {
            print(data)
        }

        }
        task.resume()
}

How does the uploadTask work? maybe it appends the data of the file to the request body and then adds the boundary automatically? if I use this code, the upload doesn't work, what I have to change?

UPDATE: I've updated the code, now the upload works in foreground using the completionHandler of the uploadTask, but if I create a background session and using URLSessionDataDelegate instead of the completionHandler (because it doesn't work in the background), the transfer rate is very slow also with a 2 MB file, how can I solve this?

UPDATE 2: with the background session, the uploadTask restarts many times and it doesn't complete, never.

3
use alamofire .KKRocks
if possible I wouldn't use any external framework to do thisRoran
the example shows dataTask (it doesn't work in the background) and downloadTask, i need working uploadTask exampleRoran
you need to configuration for background task. it is not depends on upload task or datatask.KKRocks

3 Answers

9
votes

After some attempts, I saw the URLSession.uploadTask(with: URLRequest, fromFile: URL) method attaches the file as raw body to the request, so the problem was the server counterpart that was parsing form-data requests instead raw body requests.After I fixed the server side script, the upload works in background with this code:

    var request = URLRequest(url: "my_url")
    request.httpMethod = "POST"
    request.setValue(file.lastPathComponent, forHTTPHeaderField: "filename")


    let sessionConfig = URLSessionConfiguration.background(withIdentifier: "it.example.upload")
    sessionConfig.isDiscretionary = false
    sessionConfig.networkServiceType = .video
    let session = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: OperationQueue.main)

    let task = session.uploadTask(with: request, fromFile: file)
    task.resume()
2
votes

solution as of 2020 with native URLSession to run background upload with uploadTask and multipart/form-data:

  • I followed this tutorial on setting up request for multipart/form-data as it required for my NodeJS server
  • Then I change a bit on the part for setting up URLSession:
let config = URLSessionConfiguration.background(withIdentifier: "uniqueID")
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)

// This line is important: here we use withStreamedRequest
let task = session.uploadTask(withStreamedRequest: request)

task.resume()

A little bit about my server side:

  • It's written in NodeJS with Express
  • File uploading is handled by Multer: the way I did it is really standard, you can find in many tutorials online

Hope this help

0
votes

Sometimes it is easier for the server to read file from form-data. For instance, the flask framework can read file uploaded in the format of form-data easily by request.files. Alamofire provides an easy way to do this

AF.upload(multipartFormData: { multipartFormData in
                multipartFormData.append(FilePath, withName: FilePath.lastPathComponent)
            }, to: url).responseJSON { response in
                    debugPrint(response)
            }

In this way, the file will be uploaded in the format of form-data.