使用 [defaultSession uploadTaskWithRequest:request fromFile:fileURL] 上传的文件;不在服务器端上传文件

File uploaded using [defaultSession uploadTaskWithRequest:request fromFile:fileURL]; is not uploading file at server side

我们要使用 NSURLSession 上传。 下面是 Objective C 端的代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"uploadFileServer"];

NSString *image_name=@"testing.jpg";
    image_name=[fileDest stringByAppendingPathComponent:image_name];

NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];

NSString *boundary = @"14737809831466499882746641449";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];;

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

[request setHTTPMethod:@"PUT"];

NSURLSession *defaultSession= [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:
[NSOperationQueue new]];

NSURL *fileURL =[NSURL fileURLWithPath:image_name];

NSURLSessionUploadTask *uploadTask = [defaultSession uploadTaskWithRequest:request fromFile:fileURL];

[uploadTask resume];

服务器端未收到文件。 以下是服务器端的代码:

<?php

$body = '';
$fh   = @fopen('php://input', 'r');
if ($fh)
{

  while (!feof($fh))
  {
    $s = fread($fh, 1024);
    if (is_string($s))
    {
      $body .= $s;
    }
  }
  fclose($fh);
}

?>

如果我们上传文件数据,它工作正常,但根据我的要求,我必须使用方法在后台上传文件 - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)来自文件的请求:(NSURL *)fileURL;

请指导。

检查您是否在项目选项中启用了后台模式:

终于解决了我的问题。这是解决方案:

 NSString *image_name= fileName;
    image_name=[fileDest stringByAppendingPathComponent:image_name];
    NSMutableData *imageData=[[NSMutableData alloc] init];
    NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:fileUploadURL]];
    NSString *boundary = @"14737809831466499882746641449";
    NSString *requestContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];;
    [request addValue:requestContentType forHTTPHeaderField: @"Content-Type"];
    [request setHTTPMethod:@"PUT"];
    [imageData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [imageData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"image\"; filename=\"%@\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
    [imageData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [imageData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSURL *fileURL =[NSURL fileURLWithPath:image_name];
    NSURLSessionUploadTask *uploadTask = [uploadSession uploadTaskWithRequest:request fromFile:fileURL];

    NSMutableDictionary *UploadInfo = [NSMutableDictionary dictionary];
    [UploadInfo setObject:fileURL forKey:kMZUploadKeyURL];
    [UploadInfo setObject:fileName forKey:kMZUploadKeyFileName];
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:UploadInfo options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    [uploadTask setTaskDescription:jsonString];
    [uploadTask resume];