Reputation: 31
I'm using AFnetworking for my iPhone project, i'm a beginner in Objective C.
I know how to upload multiples files using blocks, as describes here : How do you upload multiple files with AFNetworking
So into the block, we can add as many formData elements as we want.
But in my case, i do not know how many pictures i will have, since the user is free to select many. The picture's data is store into an array.
So i tried something like that into the block, but only the last element is uploaded :
for(NSDictionary *dict in imagesInfo) {
UIImage *theFile = [dict objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(theFile, 0.5);
[formData appendPartWithFileData:imageData name:@"name" fileName:@"name.jpg" mimeType:@"image/jpeg"];
}
Anyway, i was able to get it working by launching another request when the first one has finished. But i guess there is a best way to get it working ?
Tanks a lot,
Rodolphe
Upvotes: 3
Views: 4261
Reputation: 44876
You should specify a different name for each file. I suspect that will fix it.
Upvotes: 0
Reputation: 2984
AFNetworking has a couple of nice utilities for managing a "batch" of requests. There are 3 methods in particular you should consider from the AFHTTPClient class:
enqueueBatchOfHTTPRequestOperationsWithRequests //for batches enqueueBatchOfHTTPRequestOperations //for batches enqueueHTTPRequestOperation //for single request
Basically, you would loop through your array of photos and build a new array of NSMutableURLRequest (s) (pretty much like you are probably doing now). For each photo, create either a NSMutuableURLRequest or a HTTPRequestOperation and pop it in the array. The pass the new array of HTTP URL requests to AFNetworking using one of the mentioned methods.
If you want to manage the upload process as a batch use one of the batching methods. If you just want to fire and forget, use the enqueueHTTPRequestOperation which still gives you all the AFNetworking happiness.
It looks something like this:
AFHTTPClient *photoUploader = [[AFHTTPClient alloc] init];
[photoUploader enqueueBatchOfHTTPRequestOperationsWithRequests:self.photosWrappedInNSURLS progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
//block gets called after each succesful operation
//useful for messages like 1 of 3 complete
} completionBlock:^(NSArray *operations) {
//block gets called after all requests are complete
}];
if you read the doc's they will suggest you make your own subclass of AFHTTPClient and construct it as a singleton. You don't have to use it that way, but it is very handy if you have more than one type of connection back to the same host or service. I found it very useful in my last project and had very good success with it.
hope that helps be well
Upvotes: 3
Reputation: 3562
Try ASIHTTPRequest library. Using this library you can download/upload multiple files simultaneously or one by one: http://allseeing-i.com/ASIHTTPRequest/How-to-use
Upvotes: 0