1
我正在开发一个应用程序,允许用户将他们的照片上传到我的网络服务器。我最近添加了对一次上传多个文件的支持:用户可以从他们的iPhone相册中选择照片,然后上传到服务器。使用AFNetworking上传多个文件时出现CF网络错误303
上传一个文件是没有问题的,但是,当我尝试上传多个文件,我得到以下错误:
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部分处理上传文件:)
在此先感谢!
谢谢!我将在应用程序的下一次更新中更新我的框架:) – Devos50 2012-08-24 13:05:44