【问题标题】:Uploading file using NSURLSessionUploadTask to PHP server使用 NSURLSessionUploadTask 将文件上传到 PHP 服务器
【发布时间】:2014-09-30 14:37:32
【问题描述】:

我在 iOS 应用程序中有一个使用 NSURLSessionUploadTask 的视频上传系统。视频文件保存到NSURL,所以我在上传方法中使用了以下代码:

request.HTTPMethod = @"POST";
[request addValue:@"file" forHTTPHeaderField:@"fileName"];

// Create upload task
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:filePath completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if(!error) {
        // handle success
    } else {
        // handle error
    }
}];

// Run the task
[task resume];

我有一个在 nginx 下运行的 PHP 服务器(使用 Laravel)来处理这个上传。我已经用 Postman 对其进行了测试,它可以很好地接受上传(期望一个名为“file”的文件)。

当我运行上面的 objc 代码时,服务器告诉我没有上传文件($_FILES 数组为空)。

我尝试过设置和不设置“fileName”标头,并且尝试将“Content-Type”设置为“multipart/form-data”,但这些都不起作用。

如何让NSURLSessionUploadTask 正确地将这些文件(从NSURL)上传到服务器?

这篇文章似乎详细描述了一个类似的问题:NSURLSessionUploadTask not passing file to php script

【问题讨论】:

  • 您的文件中有什么?请记住,PHP 会期望正文看起来像 form-based file upload,并且 uploadTaskWithRequest 不会做任何“智能”的事情——我相信它只是将您的文件作为正文数据发送。
  • 通过仅使用 AFNetworking 并通过“手动”(base64 编码等)编写身份验证标头来解决身份验证问题解决了这个问题。

标签: php objective-c file-upload nsurlsession nsurlsessionuploadtask


【解决方案1】:

当使用 NSURLSessionUploadTask [uploadTaskWithRequest: fromFile:] 时,文件以二进制形式在请求正文中发送。

要在 PHP 中保存该文件,您只需获取请求正文并将其保存到文件中。

显然,最好进行某种文件格式验证。

Objective-C 代码:

// Define the Paths
NSURL *URL = [NSURL URLWithString:kDestinationURL];

// Create the Request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];

// Configure the NSURL Session
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.upload"];
config.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

// Define the Upload task
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:audioRecorder.url];

// Run it!
[uploadTask resume];

PHP 代码:

<?php
    // File Path to save file
    $file = 'uploads/recording.m4v';

    // Get the Request body
    $request_body = @file_get_contents('php://input');

    // Get some information on the file
    $file_info = new finfo(FILEINFO_MIME);

    // Extract the mime type
    $mime_type = $file_info->buffer($request_body);

    // Logic to deal with the type returned
    switch($mime_type) 
    {
        case "video/mp4; charset=binary":

            // Write the request body to file
            file_put_contents($file, $request_body);

            break;

        default:
            // Handle wrong file type here
    }
?>

我在这里写了一个录制音频并将其上传到服务器的代码示例: https://github.com/gingofthesouth/Audio-Recording-Playback-and-Upload

我希望这会有所帮助。

【讨论】:

  • 我通过使用 AFNetworking 找到了一种解决方法,因此无法对此进行测试,但会记住它以供将来参考,谢谢。
  • 我们可以把参数也发到这里吗?因为我想发但是没有成功
猜你喜欢
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 2016-03-31
  • 2019-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多