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.
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:
-(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.
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 🙂
Thanks in advance!
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.