iOS - How to upload a video with uploadTask?

后端 未结 3 421
Happy的楠姐
Happy的楠姐 2021-01-16 06:52

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, from

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 07:23

    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

提交回复
热议问题