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

时间:2022-02-28 15:37:16

We want to upload with NSURLSession. Below is code at Objective C 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];

At server side File is not received. Below is code at server side :

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

<?php

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

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

?>

If we upload data of file it works fine but for my requirement I have to upload file in background using method - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;

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

Please guide us.

请指导我们。

2 个解决方案

#1


0  

Check that you have enabled background mode in project options:

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

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

#2


0  

Finally I have solved my problem. Here is the solution :

最后我解决了我的问题。这是解决方案:

 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];

#1


0  

Check that you have enabled background mode in project options:

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

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

#2


0  

Finally I have solved my problem. Here is the solution :

最后我解决了我的问题。这是解决方案:

 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];