You should use uploadTaskWithRequest:fromFile:
only. The catch here is that you have to write your multipart request contents to a temp file and then upload that file.
You should use AFHTTPRequestSerializer:requestWithMultipartFormRequest:writingStreamContentsToFile:completionHandler:
.
Refer https://github.com/AFNetworking/AFNetworking/issues/1874 - Lansing's answer
Here is the sample code that worked for me:
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:TEMP_DATA_FILE];
[data writeToFile:filePath atomically:YES];
NSURL *filepathURL = [NSURL fileURLWithPath:filePath];
NSString *tempFile = [NSTemporaryDirectory() stringByAppendingPathComponent:TEMP_MULTI_PART_REQUEST_FILE];
NSURL *filePathtemp = [NSURL fileURLWithPath:tempFile];
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
NSError *error = nil;
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:AppendStrings(HOST_FOR_SERVICE_ACCESS, SERVICE_FOR_MULTIPART_UPLOAD)
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{[formData appendPartWithFileURL:filepathURL name:@"data" error:nil];}
error:&error ];
__block NSProgress *progress = nil;
[serializer requestWithMultipartFormRequest:request writingStreamContentsToFile:filePathtemp completionHandler:^(NSError *error) {
NSURLSessionUploadTask *uploadTask = [self.sessionManager uploadTaskWithRequest:request fromFile:filePathtemp progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {}];
[uploadTask resume];
Remember to clean up your temp files afterwards.