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

前端 未结 2 1066
礼貌的吻别
礼貌的吻别 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:47

    You aren't uploading what you think you are. Your intent is for the body data to be uploaded as-is. Instead, when you call uploadTaskWithRequest:fromFile:, that method effectively nils out any HTTPBody or HTTPBodyStream values in the request and replaces them with the contents of the URL that you passed in via the fromFile: parameter.

    So unless you're writing that block of form-encoded body data to that file URL somewhere else, you're uploading the file by itself instead of the multipart form data.

    You need to tweak your code to write the form data out to a file instead of storing it in HTTPBody, then pass the URL of that file to the fromFile: parameter.

提交回复
热议问题