2017-09-04 82 views
0

嗨,我已经花了超过20个小时来弄清楚如何从我的应用程序上传图片到服务器,我想要上传的图片可以从相机或照片卷.....下面是我的代码确实显示上传的进展,但犯规达到服务器,从而得到否定的答复表格服务器..请帮助我..alamofire快速上传相机和图库图片3

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); 
    } 



?> 
+0

很难立刻只要看看这段代码要弄清楚。如果这不符合您的预期,您首先必须找到真正的问题所在:服务器?或客户?通过使用第三方工具测试您的服务器,并查看服务器的响应是否与您计划的一样。如果是这样,问题出在您的客户端代码中。 – sCha

+0

嗨朋友,我的服务器代码工作正常,因为它的Android版本使用相同的代码,并且它根据需要上传图像,我在日志中获取进度信息,但是在完成后它会引发响应0.我迷失在此,任何线索真的很感谢 – Swan

回答

0

试试这个:

更换

multipartFormData.append(UIImageJPEGRepresentation(image, 0.1)!, withName: imageName) 

随着

let imgData = UIImageJPEGRepresentation(image!, 1.0)! 
multipartFormData.append(imgData, withName: "image", fileName: "image.jpg", mimeType: "image/jpg") 
+0

嗨阿卡什谢谢你花了一段时间,我已经找出了我的错误,并已经修复它,但我很欣赏你已经正确地回答了它......谢谢你有一个伟大的一天... ... – Swan