AFNetworking后台任务无效

时间:2016-06-07 05:50:24

标签: ios objective-c json web-services afnetworking

以下是我在带有背景功能的服务器中发布图片的代码,但它提供了错误

 NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.uploadDocument.Background"];


MBProgressHUD *progres = [MBProgressHUD showHUDAddedTo:[[AppDelegate sharedInstance] window] animated:YES];
progres.mode = MBProgressHUDModeDeterminateHorizontalBar;
progres.progress = 0.0;

if (!self.sessionManager) {
    _sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];

}
NSError *error;
NSMutableURLRequest *requet= [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"url_post" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    NSError *error1;
    [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:@"fileUpload" fileName:path mimeType:@"image/jpeg" error:&error1];
    NSLog(@"%@",error1);
    NSLog(@"%@",filePath);

} error:&error];
NSLog(@"%@",requet);
NSURLSessionUploadTask *uploadTask = [self.sessionManager uploadTaskWithRequest:requet fromFile:[NSURL fileURLWithPath:filePath] progress:^(NSProgress * _Nonnull uploadProgress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [progres setProgress:uploadProgress.fractionCompleted];
    });

    NSLog(@"%f",uploadProgress.fractionCompleted);
} completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    NSLog(@"%@",response);
    NSLog(@"%@",error);
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[AppDelegate sharedInstance].window animated:YES];
    });

}];

[uploadTask resume];

响应代码= -1011“请求失败:内部服务器错误(500)”UserInfo = {com.alamofire.serialization.response.error.response =

“MIME多部分流的意外结束.MIME多部分消息未完成。”在.NET错误中

请帮我解决

这是有效但没有后台配置。

[self.sessionManager POST:@"URL " parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:[NSData dataWithContentsOfFile:filePath]
                                        name:@"fileUpload"
                                    fileName:path mimeType:@"image/jpeg"];

        } progress:^(NSProgress * uploadProgress) {

            NSLog(@"%f",uploadProgress.fractionCompleted);
            dispatch_async(dispatch_get_main_queue(), ^{
                [progres setProgress:uploadProgress.fractionCompleted];
            });
        } success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"Response: %@", responseObject);
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:[AppDelegate sharedInstance].window animated:YES];
            });

        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"Error: %@", error);
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:[AppDelegate sharedInstance].window animated:YES];
            });
        }];

0 个答案:

没有答案