2016-11-10 103 views
1

當我嘗試將圖像更新爲我的Db時,我遇到了一個很大的問題。Multipart Image Alamofire 4 swift 3

我必須上傳一個圖片和一些字符串值。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      imageCropped = selectedImage 
      self.sendImageWithMultipart() 
      imagePicker.dismiss(animated: true, completion: { _ in }) 
     } 
    } 

func sendImageWithMultipart() {   
     Alamofire.upload(multipartFormData: { 
      multipartFormData in 
      multipartFormData.append("\(HGYGjihf746fg743g)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"auth_token") 
      multipartFormData.append("\(0)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os") 
      multipartFormData.append("\(10.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os_version") 
      multipartFormData.append("\(3.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"app_version") 

      if let photo = self.imageCropped, let jpegImage = UIImageJPEGRepresentation(photo, 50.0) { 
       print(jpegImage) 
       multipartFormData.append(jpegImage, withName: "profile_photo", mimeType: "image/*") 
      } 
     }, to: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)", method:.post, headers:["Content-Type":"multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__"], encodingCompletion: { 
      encodingResult in 
      switch encodingResult { 
      case .success(request: let upload, streamingFromDisk: _, streamFileURL: _): 
       upload.validate().responseJSON { 
        response in 
        if response.result.isFailure { 
         debugPrint(response) 
        } else { 
         debugPrint(response) 
        } 
       } 
      case .failure(let encodingError): 
       NSLog((encodingError as NSError).localizedDescription) 
      } 
     }) 
    } 

但結果是這樣的:

[Request]: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)" 
[Response]: <NSHTTPURLResponse: 0x17023c0e0> { URL: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)" } { status code: 500, headers { 
    "Cache-Control" = "no-cache, private"; 
    Connection = close; 
    "Content-Length" = 5545; 
    "Content-Type" = "text/html; charset=UTF-8"; 
    Date = "Thu, 10 Nov 2016 10:49:00 GMT"; 
    Server = "Apache/2.4.18 (Amazon) OpenSSL/1.0.1k-fips PHP/5.6.21"; 
    "X-Powered-By" = "PHP/5.6.21"; 
} } 
[Data]: 5545 bytes 
[Result]: FAILURE: responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500)) 
[Timeline]: Timeline: { "Request Start Time": 500467740.667, "Initial Response Time": 500467740.793, "Request Completed Time": 500467743.733, "Serialization Completed Time": 500467743.733, "Latency": 0.126 secs, "Request Duration": 3.066 secs, "Serialization Duration": 0.000 secs, "Total Duration": 3.066 secs 

的問題是如何我將圖像轉換或內容類型還是什麼?

謝謝

+0

如何顯式指定MIME類型:'multipartFormData.append(jpegImage,withName:「profile_photo.jpeg」,mimeType:「image/jpeg」)' – Enix

+0

你能解決? – Cmag

+0

我無法解決問題。我也試過指定mimetype,但沒有任何內容 –

回答

0

這可能會潛在地解決問題:

let endpoint = "https://api.blah/images/" + id + "/documents" 

let imageData = UIImagePNGRepresentation(image) 

Alamofire.upload(
      multipartFormData: { multipartFormData in 
       multipartFormData.append(type.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"type") 
       multipartFormData.append(imageData!, withName: "file.png", mimeType: "image/png") 
     }, to: endpoint, method:.post, headers:headers, encodingCompletion: { encodingResult in 
      switch encodingResult { 
      case .success(request: let upload, streamingFromDisk: _, streamFileURL: _): 
       upload.validate().responseJSON { (response) in 
        if response.result.isFailure { 
         debugPrint(response) 
        } else { 
         debugPrint(response) 
        } 
       } 
      case .failure(let encodingError): 
       NSLog((encodingError as NSError).localizedDescription) 
      } 
     }) 

似乎達到我的服務器,但對我來說,我得到一個400回(可能失敗TV4驗證)。

+0

大部分看起來像一個答案,但如果您有相關問題,請將其作爲一個新問題。問題材料大多不會在這裏被看到,顯然是一個問題的材料通常會被版主刪除。 – halfer

+0

但我必須使用圖片?而不是圖片的網址? –

相關問題