0

I am using NSURLSessionUploadTask to upload a image in Background using NSURLSession with backgroundSessionConfiguration.

I am sharing the below code snippets of IOS and PHP where I used to upload images[raw data] to server with PHP backend. And this file uploads in background.

       - (void)uploadImage:(NSURL*)image
       {
       NSString *urlString  =[NSString    stringWithFormat:@"www.dummy.com/upload.php"];
       NSURL *url   =[[NSURL alloc]initWithString:urlString];
       // 1
      static NSURLSession *upLoadSession = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
      NSInteger randomNumber = arc4random() % 1000000;
  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:[NSString stringWithFormat:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession%ld",(long)randomNumber]];
  upLoadSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});

       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
       [request setHTTPMethod:@"POST"];

       // 3
       self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:image];

       // 5
       [self.uploadTask resume];
        }

PHP function--

         $putdata = fopen("php://input", "r");

         /* Open a file for writing */
         $project_id = $_REQUEST["api"];
         $fp = fopen($project_id.".MOV", "w");

          /* Read the data 1 KB at a time
          and write to the file */
          while ($data = fread($putdata, 1024))
          fwrite($fp, $data);

         /* Close the streams */
         fclose($fp);
         fclose($putdata);


  >>>>3)I get response on logs on IOS from task.response-


   Connection = "keep-alive";
   "Content-Length" = 309;
   "Content-Type" = "text/html; charset=UTF-8";
   Date = "Wed, 02 Apr 2014 03:49:04 GMT";
   Server = "Apache/2.2.15 (Red Hat)";
   Via = "1.0 Proxy6-B24:3128 (squid/2.6.STABLE21)";
   "X-Cache" = "MISS from Proxy6-B24";
   "X-Cache-Lookup" = "MISS from Proxy6-B24:3128";
   "X-Powered-By" = "PHP/5.3.3";

I hope this helps someone in need.

  • Well, your PHP is expecting a standard form post, so are you encoding your data as multipart/form-data? See [this answer](http://stackoverflow.com/questions/19789013/how-to-use-nsurlsession-to-post-some-properties-and-a-image) for some depth on that. Looks like you're not setting a Content-Type, either... [This](http://stackoverflow.com/questions/20893171/asynchronous-upload-with-nsurlsession-will-not-work-but-synchronous-nsurlconnect) is probably the closest working example I can see on SO to what you're doing; though it's not using a background upload, the principles are the same. – Matt Gibson Apr 02 '14 at 07:27
  • Matt thanks for your help, I found myself while struggling with the code. If you can share with me how can I get response data from the server, for now I just get response headers only. – Virendra Vaishnav Apr 02 '14 at 18:58

0 Answers0