【发布时间】:2017-02-22 01:05:16
【问题描述】:
我通过 Alamofire 使用 multipart 上传图像,但构建和上传失败 这是我的快速代码”
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(d1, withName: "file",fileName: "file.jpg", mimeType: "image/jpg")// d1 in let d1 = UIImageJPEGRepresentation(uiimage, 1)
},
to: "mysite/upload.php",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
还有我的 php 代码
$ip = $_SERVER['REMOTE_ADDR'];
$timestp = DATE("Y-m-d H:i:s");
$milliseconds = round(microtime(true) * 1000);
$image_name=date("dmyHis").$milliseconds.$psmemid.$psplace.$pscate.".jpg";
$imgpath = "../Image/";
$newname = $imgpath.$image_name;
$copied = copy($_FILES['club_image']['tmp_name'], $newname);
$upload = $imgpath . basename($_FILES['club_image']['tmp_name']);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
上传后的日志是
[Response]: <NSHTTPURLResponse: 0x600000428f40> { URL: mysite/upload.php } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Thu, 13 Oct 2016 04:33:03 GMT";
Server = nginx;
Vary = "User-Agent";
"X-Powered-By" = "PHP/5.3.29";
} }
[Data]: 0 bytes
[Result]: SUCCESS:
[Timeline]: Timeline: { "Request Start Time": 498025982.810, "Initial Response Time": 498025982.843, "Request Completed Time": 498025983.432, "Serialization Completed Time": 498025983.432, "Latency": 0.033 secs, "Request Duration": 0.622 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.622 secs }
感谢您的帮助。
【问题讨论】:
-
也许你应该添加错误日志
-
@iDeveloper 我使用 multipart 是因为我在旧堆栈问题中看到用于上传它的成功(我使用
Alamofire.upload(fileURL, to: "https://httpbin.org/post").responseJSON { response in debugPrint(response) }它同样失败) -
@YasuhiroKondo 您应该将
responseJSON更改为responseString以获得更多错误细节。也许是它的服务器端问题 -
@NhatDinh 我更新了它