unable to upload file using NSURLSession multi-part form data in iOS

前端 未结 2 1072
礼貌的吻别
礼貌的吻别 2021-02-19 01:58

I am trying to upload a video / image file using- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL; method using m

2条回答
  •  逝去的感伤
    2021-02-19 02:20

    To prevent wasting time dealing with it.

    The complete snippet based on @dgatwood answer

    private func http(request: URLRequest){
            let configuration = URLSessionConfiguration.default
            let session = URLSession(configuration: configuration, delegate: self, delegateQueue: .main)
            /*Tweaking*/
            let task = session.uploadTask(with: request, from: request.httpBody!)
            task.resume()
        }
    

    And.. do not forget to add the Headers on request object like

    request.setValue("multipart/form-data; boundary=\(yourboundary)", forHTTPHeaderField: "Content-Type")
    

提交回复
热议问题