1

I want to send max. 100 images in a single request to my server. Simple image upload works fine for me.

But I want to create a stream of data with AFNetworking 2.0 api. So, I want to ask that How I can create a newtwork stream with my image data.

Also, This stream should work in background mode of the app also & images should upload on the server in background mode of the app. How can I achieve this with AFNetworking 2.0 library?

Any suggestions will be helpful.

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
Nirmit Dagly
  • 1,272
  • 1
  • 12
  • 25

3 Answers3

2

I do not think it is a good idea but I will answer your question to let you decide yourself.

AFAIK there are only two ways to send 100 images with a single request:

  1. Bind them in a single file, for example zip them
  2. Use multipart/form-data

First approach is quite obvious :) Let's talk about the second one.

Create a multipart request with AFNetworking:

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]
                                    multipartFormRequestWithMethod:@"POST"
                                    URLString:[url absoluteString]
                                    parameters:nil
                                    constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                                    {
                                        for (NSURL *imageUrl in imageURLsArray)
                                            [formData appendPartWithFileURL:imageUrl name:[imageUrl lastPathComponent] error:nil];
                                    }
                                    error:nil];

Save request body to a file because background uploading is supported only for files, not for streams or data in a memory:

NSURL *fileURL = create url for a temporary file
[[AFHTTPRequestSerializer serializer] 
    requestWithMultipartFormRequest:request
    writingStreamContentsToFile:fileURL
    completionHandler:(void (^)(NSError *error))
    {
        NSMutableURLRequest *newRequest = [request mutableCopy];
        newRequest.HTTPBodyStream = nil;
        // Here you can start background uploading of file fileURL using newRequest request

}];

How to upload a file in background you can find for example here AFNetworking 2.0 and background transfers or here http://www.objc.io/issue-5/multitasking.html

Community
  • 1
  • 1
Avt
  • 16,927
  • 4
  • 52
  • 72
  • Hello Avt, If I create a zip file of 100 images then also It'll be having same size as of 100 images. So, It is not a convenient way for me. – Nirmit Dagly Apr 15 '15 at 05:21
  • @NirmitDagly Zip images is a way to send 100 images in a ONE request as a single file. The fact it will not reduce total size is not important here. As you can see I wrote "Bind them in a single file, for EXAMPLE zip them", so main idea of this point is to BIND images in a single file. – Avt Apr 15 '15 at 08:37
1

I'd advise against a single request uploading 100 images because if it fails, the entire request fails. I'd be inclined to do individual requests. A single request may have an intuitive appeal because it is simpler, but it's going to be less graceful in the presence of network interruptions.

In terms of doing background uploads with AFNetworking, see AFNetworking 2.0 and background transfers, which walks through a few of the considerations (using AFHTTPSessionManager instead of AFHTTPRequestOperationManager, writing the necessary app delegate methods to capture background session completion handler, the fact that you cannot use task-specific completion block handlers, etc.).

Note, you will want to create upload tasks that upload a file. This means that if your web service is expecting a multipart/formdata request, that you should stage these requests to a file (see Avt's answer) and then supply that as the file parameter of the upload request. This not only is compatible with background sessions, but it has a minimal peak memory usage profile (which I assume was your intent behind your comment about wanting a stream-based request).

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

@Nirmit, you can upload larger number of images using multipart uploading. kindly followed the link. https://github.com/AFNetworking/AFNetworking.

while uploading set of images, you need to set timeout limit at your end as well as server end at max level. also you need to provide background running support. incase user enter in background mode.

In multipart request form:

you need to add for-loop of images instead of single image. here i added modification of multipart request form.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};

[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

for(int i=0:i<1000:i++){

 NSURL *filePath = [NSURL fileURLWithPath:arrayImages[i]];

[formData appendPartWithFileURL:filePath name:@"image" error:nil];

}




} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
  • 1
    You cannot use `AFHTTPRequestOperationManager` in conjunction with background sessions. Likewise, you cannot use `POST` method in conjunction with background session. – Rob Apr 13 '15 at 14:48