AFNetworking error in uploadTaskWithStreamedRequest

Error

Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks in background sessions must be from a file'

when i try

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

configurationfor NSURLSessionworks fine, but when I use below configurationthen the application crashes and give me an error.

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:kBackgroundSessionIdentifier];
0
source share
2 answers

You should use uploadTaskWithRequest: request fromFile: only. The trick here is that you need to write the contents of a multi-page request to a temp file, and then load that file.

AFHTTPRequestSerializer: requestWithMultipartFormRequest: writingStreamContentsToFile: completeHandler:. . https://github.com/AFNetworking/AFNetworking/issues/1874 - Lansing

, :

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];

.

+2

, : " " - .

.

[NSURLSessionConfiguration backgroundSessionConfiguration:kBackgroundSessionIdentifier];

uploadTaskWithStreamedRequest:, uploadTaskWithRequest:request fromFile:

Apple . , , URL- .

0

All Articles