使用AFNetworking上传多个文件时出现CF网络错误303

时间:2021-02-21 06:59:36

I'm developing an app that allows users to upload their photos to my web server. I've recently added support for uploading multiple files at once: users can select photos from their iPhone album and upload then to the server.

我正在开发一个允许用户将照片上传到我的网络服务器的应用程序。我最近添加了对同时上传多个文件的支持:用户可以从他们的iPhone相册中选择照片然后上传到服务器。

Uploading one file is no problem, however, when I try to upload multiple files, I get the following error:

上传一个文件没问题,但是,当我尝试上传多个文件时,我收到以下错误:

The operation couldn't be completed (kCFErrorDomainCFNetwork error 303.)

The code I'm using for uploading the files is the following:

我用来上传文件的代码如下:

// start the uploading for each photo
for(int i = 0; i < photosArray.count; i++)
{
    Photo *currentPhoto = [photosArray objectAtIndex:i];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"upload", @"command", UIImageJPEGRepresentation(currentPhoto.image,70),@"file", [NSNumber numberWithInt:album.albumId], @"albumId", currentPhoto.title, @"title", currentPhoto.description, @"description", nil];

    [[AFAPI sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json)
     {
         //completion
         if (![json objectForKey:@"error"]) 
         {
             // hide the UIProgressView and show the detail label
             UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
             UIProgressView *pv = (UIProgressView *) [cell viewWithTag:4];
             pv.hidden = YES;

             UILabel *detailLabel = (UILabel *) [cell viewWithTag:3];
             detailLabel.text = @"Upload completed";
             detailLabel.hidden = NO;

         } 
         else 
         {
             //error :(
             NSString* errorMsg = [json objectForKey:@"error"];
             [UIAlertView error:errorMsg];
         }
     }
     onUploadProgress:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) 
    {
         // ...
    }];
}

I'm enumerating the photosArray with a for loop and for every photo it finds, it uploads the image (currentPhoto.image). The implementation of the commandWithParams function is:

我正在使用for循环枚举photosArray,并且对于它找到的每张照片,它都会上传图像(currentPhoto.image)。 commandWithParams函数的实现是:

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock onUploadProgress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))uploadProgressBlock
{
    NSData* uploadFile = nil;
    if ([params objectForKey:@"file"]) 
    {
        uploadFile = (NSData*)[params objectForKey:@"file"];
        [params removeObjectForKey:@"file"];
    }

NSMutableURLRequest *apiRequest = 
[self multipartFormRequestWithMethod:@"POST" 
                                path:kAPIPath 
                          parameters:params 
           constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
               if (uploadFile) {
                   [formData appendPartWithFileData:uploadFile 
                                               name:@"file" 
                                           fileName:@"photo.jpg" 
                                           mimeType:@"image/jpeg"];
               }
           }];

AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setUploadProgressBlock:uploadProgressBlock];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
}

Sorry for the long code part but I really can't figure out how to solve this error. I've tried to use a NSOperationQueue but that also gave the same error.

很抱歉长代码部分,但我真的无法弄清楚如何解决此错误。我试过使用NSOperationQueue,但也犯了同样的错误。

I've searched on internet, and I figured out that error 303 in the CFNetwork means that the HTTP response couldn't be parsed. Could it be that the problem is in the web service? If so, I can also give the php part where I handle the uploaded file :)

我在互联网上搜索过,我发现CFNetwork中的错误303意味着无法解析HTTP响应。难道问题出在Web服务中吗?如果是这样,我也可以给我处理上传文件的php部分:)

Thanks in advance!

提前致谢!

1 个解决方案

#1


1  

I think that this was a bug in AFNetworking. I had the exact same issue, but upgrading to the latest version downloaded on August 24, 2012 resolved the problem for me.

我认为这是AFNetworking中的一个错误。我有完全相同的问题,但升级到2012年8月24日下载的最新版本解决了我的问题。

#1


1  

I think that this was a bug in AFNetworking. I had the exact same issue, but upgrading to the latest version downloaded on August 24, 2012 resolved the problem for me.

我认为这是AFNetworking中的一个错误。我有完全相同的问题,但升级到2012年8月24日下载的最新版本解决了我的问题。