Post 使用带有backgroundSessionConfiguration的NSURLSession发布后台视频

Post 使用带有backgroundSessionConfiguration的NSURLSession发布后台视频,post,background,nsurlsession,Post,Background,Nsurlsession,我正在使用nsurlsessionpluadtask使用NSURLSession和backgroundSessionConfiguration在后台上传图像 我正在分享下面的IOS和PHP代码片段,我在这里使用PHP后端将图像[原始数据]上传到服务器。这个文件在后台上传 - (void)uploadImage:(NSURL*)image { NSString *urlString =[NSString stringWithFormat:@"www

我正在使用
nsurlsessionpluadtask
使用
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函数--


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

好吧,您的PHP需要一个标准表单post,那么您是否将数据编码为多部分/表单数据?看看有没有深度。看起来您也没有设置内容类型。。。这可能是我能看到的最接近你所做的工作的例子;虽然它不使用后台上传,但原理是一样的。Matt谢谢你的帮助,我发现自己在代码中挣扎。如果您可以与我分享如何从服务器获取响应数据,目前我只获取响应头。
         $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";