Multipart Image Alamofire 4 swift 3

问题描述:

当我尝试将图像更新为我的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,但没有任何内容 –

这可能会潜在地解决问题:

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

但我必须使用图片?而不是图片的网址? –