1

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

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

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

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

// 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) 
    {
         // ...
    }];
}

我用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];
}

很抱歉代码部分很长,但我真的不知道如何解决这个错误。我尝试使用 NSOperationQueue 但这也给出了同样的错误。

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

提前致谢!

4

1 回答 1

1

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

于 2012-08-24T12:07:31.103 回答