【发布时间】:2015-09-16 19:18:05
【问题描述】:
下面是用于上传图像文件的 sn-p。我只想使用 NSURLSessionUploadTask,因为它提供了我想在我的应用程序中使用的后台上传功能。
我还想将参数与文件上传一起发布。
我也不擅长服务器端代码。谁能指导我做错了什么。
上传代码
NSString* filePath = [[NSBundle mainBundle]
pathForResource:@"sample" ofType:@"jpg"];
uint64_t bytesTotalForThisFile = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL] fileSize];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:uploadPath];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%llu", bytesTotalForThisFile] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
[request setTimeoutInterval:30];
NSString* filePath = [[NSBundle mainBundle]
pathForResource:@"sample" ofType:@"jpg"];
NSURLSessionUploadTask *taskUpload= [self.uploadSession uploadTaskWithRequest:request fromFile:[[NSURL alloc] initFileURLWithPath:filePath]];
PHP 代码
<?php
$objFile = & $_FILES["file"];
$strPath = basename( $objFile["name"] );
if( move_uploaded_file( $objFile["tmp_name"], $strPath ) ) {
print "The file " . $strPath . " has been uploaded.";
} else {
print "There was an error uploading the file, please try again!";
}
从服务器端我得到 $_FILES 的空数组
【问题讨论】:
标签: php ios file-upload nsurlconnection nsurlsession