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