2017-09-04 80 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