使用带有backgroundSessionConfiguration的NSURLSession来POST背景vidoes PHP

时间:2014-04-02 04:27:35

标签: post background nsurlsession

我正在使用NSURLSessionUploadTask使用NSURLSession backgroundSessionConfiguration在背景中上传图片。

我正在分享IOS和PHP的以下代码片段,我曾经使用PHP后端将图像[原始数据]上传到服务器。此文件在后台上传。

       - (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函数 -

         $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";

我希望这可以帮助有需要的人。

0 个答案:

没有答案