我对NSURLSessionUploadTask有一些问题。我尝试上传我使用UIImagePickerController选择的图片。我有一个PHP页面,目前要么发送回真实的'如果文件存在,那么' false'它没有。我收到了假的。我已经用Postman测试了PHP页面,它运行得很好。因此,我的问题在于iOS。
这里是文件上传:
func uploadFile(fileData: NSData) {
var request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "https://www.codekaufman.com/file.php")!)
request.HTTPMethod = "POST"
let session = NSURLSession(configuration: .defaultSessionConfiguration())
let uploadTask = session.uploadTaskWithRequest(request, fromData: fileData, completionHandler:{
data, response, error in
if error != nil {
println(error)
return
}
println(response.description)
println()
var err: NSError?
var receivedData: [String]!
receivedData = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? [String]
if(receivedData != nil) {
println(receivedData)
} else {
println("No data recieved.")
}
})
uploadTask.resume()
}
这里的PHP只是为了踢:
<?php
error_reporting(E_ALL);
ini_set('display errors', 1);
if(isset($_FILES['file'])) {
echo json_encode('true');
} else {
echo json_encode('false');
}
?>
这是NSURLResponse:
<NSHTTPURLResponse: 0x7ff4f062bd10> { URL: https://www.codekaufman.com/file.php } { status code: 403, headers {
"Accept-Ranges" = bytes;
Connection = "Keep-Alive";
"Content-Length" = 1551;
"Content-Type" = "text/html";
Date = "Fri, 13 Feb 2015 08:05:55 GMT";
"Keep-Alive" = "timeout=10, max=200";
Server = Apache;
} }
No data recieved.