【发布时间】:2018-02-12 20:10:00
【问题描述】:
嗨,我花了 20 多个小时来弄清楚如何将图像从我的应用程序上传到服务器,我要上传的图像可以是从相机或照片胶卷中拍摄的.....这是我的代码确实显示了上传的进度,但没有到达服务器,因此得到了否定的响应表单服务器..请帮助我..
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(UIImageJPEGRepresentation(image, 0.1)!, withName: imageName)
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to: URL_USER_PROFILE_IMG_UPLOAD)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
}
case .failure(let encodingError):
print(encodingError)
}
}
我的服务器代码是 PHP 的
<?php
// Path to move uploaded files
$target_path = "profile-photos/";
// array for final json respone
$image_upload = array();
$server_ip ="00.000.000.000";
//gethostbyname(gethostname());
// final file url that is being uploaded
$file_upload_url = 'http://' . $server_ip .'/'.'folder2017'.' /'.'webser'.'/'. $target_path;
if(isset($_FILES['image']['name'])) {
$target_path=$target_path.$_FILES['image']['name'];
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
$image_upload=array('path'=>$file_upload_url,'response_code'=>1);
echo json_encode($image_upload);
}
} else {
$image_upload=array('response_code'=>0);
echo json_encode($image_upload);
}
?>
【问题讨论】:
-
仅通过查看这段代码很难立即弄清楚。如果这没有按您的预期工作,您首先必须找到真正的问题在哪里:服务器?还是客户?使用 3rd 方工具测试您的服务器,并查看服务器是否按您的计划响应。如果是这样,则问题出在您的客户端代码中。
-
嗨朋友,我的服务器代码工作正常,因为它与 android 版本使用的代码相同,并且它确实根据需要上传图像,我在日志中获取进度信息,但完成后它会抛出响应 0。我我迷失了,任何线索将不胜感激
标签: ios iphone swift3 alamofire