【发布时间】:2015-04-17 16:24:15
【问题描述】:
我如何从 Swift 中获取参数值,文件上传已经工作,我试过 $_GET["familyId"],但没有成功?
斯威夫特:
let manager = AFHTTPRequestOperationManager()
let url = "http://localhost/test/upload.php"
var fileURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("test_1", ofType: "mov")!)
var params = [
"familyId":locationd,
"contentBody" : "Some body content for the test application",
"name" : "the name/title",
"typeOfContent":"photo"
]
manager.POST( url, parameters: params,
constructingBodyWithBlock: { (data: AFMultipartFormData!) in
println("")
var res = data.appendPartWithFileURL(fileURL, name: "fileToUpload", error: nil)
println("was file added properly to the body? \(res)")
},
success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
println("Yes thies was a success")
},
failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
println("We got an error here.. \(error.localizedDescription)")
})
PHP:
$target_dir = "uploads/";
$target_dir = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir)) {
echo json_encode([
$user_info;
"Message" => "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.",
"Status" => "OK",
]);
【问题讨论】:
-
您尚未将该 id 添加到 url。你应该看看
$_POST['familyId'] -
@MarcB 我不明白?看看哪里?
-
等一下,让我说对了,您使用的是 HTTP 方法 POST,对吗?为什么要尝试使用 GET 而不再使用 POST?
标签: php swift afnetworking