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

안녕하세요 친구, 내 서버 코드가 잘 작동하고 있기 때문에 동일한 코드가 안드로이드 버전에 사용되고 원하는대로 이미지를 업로드합니다. 로그에서 진행 정보를 얻을 수 있지만 완료 후 응답이 0이됩니다. 길을 잃어 버렸습니다. 정말 감사하겠습니다 – Swan

답변

0

시험해보기 :

,210

그것은 즉시 바로이 코드를보고 파악하기 어렵다

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

안녕하세요 akash 시간을내어 주셔서 감사합니다, 내가 내 실수를 알아 냈어 그리고 이미 그것을 고쳤지 만, 나는 당신이 바르게 그것을 대답 ... 감사합니다 좋은 하루 되세요 ... – Swan

관련 문제