使用NSURLSessionUploadTask将文件上载到PHP服务器
我在iOS应用程序中有一个视频上传系统,使用使用NSURLSessionUploadTask将文件上载到PHP服务器,php,objective-c,file-upload,nsurlsession,nsurlsessionuploadtask,Php,Objective C,File Upload,Nsurlsession,Nsurlsessionuploadtask,我在iOS应用程序中有一个视频上传系统,使用NSURLSessionUploadTask。视频文件保存到NSURL,因此我在上传方法中使用以下代码: request.HTTPMethod = @"POST"; [request addValue:@"file" forHTTPHeaderField:@"fileName"]; // Create upload task NSURLSessionUploadTask *task = [session uploadTaskWithRequest:r
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代码时,服务器告诉我没有上传任何文件($\u FILES
数组为空)
我尝试过设置“fileName”标题和不设置“fileName”标题,我尝试过将“Content Type”设置为“multipart/formdata”,但这些都不起作用
如何让NSURLSessionUploadTask
将这些文件(从NSURL
)正确上载到服务器
这篇文章似乎详细介绍了一个类似的问题:当使用nsurlsessionPloadTask[uploadTaskWithRequest:fromFile:]时,文件在请求正文中作为二进制文件发送 要在PHP中保存该文件,只需获取请求主体并将其保存到文件 显然,最好进行某种文件格式验证 目标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
// 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
}
?>
PHP代码:
// 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
// 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
}
?>
我在这里编写了一个录制音频并将其上传到服务器的代码示例:
我希望这有帮助 使用NSURLSessionPloadTask[uploadTaskWithRequest:fromFile:]时,文件在请求正文中作为二进制文件发送 要在PHP中保存该文件,只需获取请求主体并将其保存到文件 显然,最好进行某种文件格式验证 目标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
// 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
}
?>
PHP代码:
// 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
// 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
}
?>
我在这里编写了一个录制音频并将其上传到服务器的代码示例:
我希望这有帮助 你的档案里有什么?请记住,PHP会希望主体看起来像一个,而uploadTaskWithRequest不会做任何“聪明”的事情——我相信它只是将您的文件作为主体数据发送。通过使用AFNetworking和通过“手动”写入auth头来绕过身份验证问题(base64编码等)解决了这一问题(某种程度上).你的档案里有什么?请记住,PHP会希望主体看起来像一个,而uploadTaskWithRequest不会做任何“聪明”的事情——我相信它只是将您的文件作为主体数据发送。通过使用AFNetworking和通过“手动”写入auth头来绕过身份验证问题(base64编码等)解决了这一问题(某种程度上).我通过使用AFNetworking找到了解决方法,因此无法测试此问题,但将记住它以备将来参考,谢谢。我们是否也可以在此发布参数?因为我正在尝试发布但未发现成功我通过使用AFNetworking找到了解决方法,因此无法测试此问题,但将记住它以备将来参考,谢谢。我们可以在这里发布参数吗?因为我正在尝试发布,但没有发现成功