2

Step 1: here I am creating the Request

NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"
                                                                                           URLString:[NSString stringWithFormat:@"%@%@", API_MAIN_URL, IMAGE_UPLOAD]
                                                                                          parameters:param constructingBodyWithBlock:^(id formData) {

           [formData appendPartWithFileURL:[NSURL fileURLWithPath:strImagePath] 
                                      name:@"sendimage" 
                                  fileName:[strImagePath lastPathComponent] 
                                  mimeType:@"image/png"
                                     error:nil];
                                       } error:nil];
[request1 setValue:authToken forHTTPHeaderField:@"Authorization"];

Step 2: here I am creating the Stream at given Location

[[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:request1 
                                          writingStreamContentsToFile:[NSURL fileURLWithPath:[strImagePath stringByDeletingPathExtension]] 
                                                    completionHandler:^(NSError *error){

Step 3: here I am creating uploading task.

        ///here is file
        NSProgress *progress = nil;
        NSURLSessionUploadTask *uploadTask = [self.manager uploadTaskWithRequest:request1
                                                                        fromFile:[NSURL fileURLWithPath:strImagePath]
                                                                        progress:&progress
                                                               completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                                                                       NSLog(@"response : %@\n\n responseObject : %@\n\n error : %@", response, responseObject, error);

                                                               }];
        [uploadTask resume];
    }
                                                    }];
}

My problem is before application going to background mode I want to write all the request HTTPBody(HTTPStream) at given location with using Step:1 and Step:2 and save all the request into the NSArray after writing all the HTTPStream in File(in application Foreground) mean while I'll show Image prepare to upload.

then I'll start creating background task with the help of Step3: with passing request into this which request I have stored into the NSArray. with this approach I am not able to upload images.

But If I call all the steps together one by one then it will upload the image on the server, but with this approach my application should be in Foreground for create request because we need to write the HTTPBody at give location.

Please help me to come out this problem I am stuck on this from last 2 week. My application needed more then 500 images uploading over the server.

Ashish Kakkad
  • 22,149
  • 11
  • 88
  • 130
Sumeet Mourya
  • 410
  • 1
  • 7
  • 21
  • 500 images uploading over the server. at a time? – Nitin Gohel May 15 '15 at 05:34
  • http://stackoverflow.com/a/19841957/2518805 try this – sanjeet May 15 '15 at 05:36
  • @NitinGohel that application is album printing of those photos which you have selected, then do the needful things after that start uploading with background upload task. we create the background task for every single image. – Sumeet Mourya May 15 '15 at 05:37
  • @sanjeet this will work only if the application is in foreground, because when you want to upload this much number of images in over the server we need some background process which possible only with the **Background Transfer services** [link](http://code.tutsplus.com/tutorials/ios-7-sdk-background-transfer-service--mobile-20595) – Sumeet Mourya May 15 '15 at 05:38
  • Check this : http://stackoverflow.com/questions/8861390/ios-background-downloads-when-the-app-is-not-active for setting long task when you app in background – Nitin Gohel May 15 '15 at 05:42

2 Answers2

0

try like this

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
            } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        NSProgress *progress = nil;

        NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                NSLog(@"%@ %@", response, responseObject);
            }
        }];

        [uploadTask resume];
  • This request is using Stream, and for background uploading task we need to use the FileURL `uploadTaskWithRequest:request fromFile` [Apple Doc](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html) – Sumeet Mourya May 15 '15 at 05:49
0

I have done with this task, create all the upload task without resume them and save every task within the NSArray after creation. Once all the task are created then call a method in which we resume all the tasks.

Sumeet Mourya
  • 410
  • 1
  • 7
  • 21
  • I am working on the same requirement. Can you please share code to achieve same in background mode? I want to upload up to 100 photos to the server. – Nirmit Dagly Dec 04 '15 at 13:13
  • @NirmitDagly Can you try with this. [link]http://stackoverflow.com/questions/30215551/how-to-upload-more-then-200-images-in-background-by-afnetworking-in-ios/30342479?noredirect=1#comment56001855_30342479 because I implement that code in project not like separate project or Library. – Sumeet Mourya Dec 10 '15 at 06:50