我正在开发一款允许用户将照片上传到我的网络服务器的应用。我最近添加了对同时上传多个文件的支持:用户可以从他们的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响应。难道问题出在Web服务中吗?如果是这样,我也可以给我处理上传文件的php部分:)
提前致谢!
答案 0 :(得分:1)
我认为这是AFNetworking中的一个错误。我有完全相同的问题,但升级到2012年8月24日下载的最新版本解决了我的问题。