Alamofire 4文件上传参数问题

问题描述:

普莱舍看看这里从邮递员 POSTMAN success result 的知情同意,但代码不能运行,我失去了一些东西,请帮我出Alamofire 4文件上传参数问题

var parameters = [String: String]() 
     parameters = [ 
      "profile_image": "imagefile" 
     ] 

     let headers: HTTPHeaders = [ 
      "Accept": "application/json" 
     ] 

     let url = "http://******/public/api/v1/profile/update-picture?api_token=" + "aySlC26ZTHtlnS0lhUpdghxkd9gKJBLXLYFUO2Jidmiisoka9iFIicwRIZFx" 
     let completeUrl = URL(string:url)! 

     let imageData = UIImageJPEGRepresentation(chosenImage!, 1) 
     print ("image data:: \(imageData)") 
     print ("chosenImage:: \(chosenImage)") 

//  Alamofire.upload(imageData!, to: completeUrl).response { response in 
//   print (response) 
//  } 

     Alamofire.upload(
      multipartFormData: { 
       multipartFormData in 
       multipartFormData.append(imageData!, 
             withName: "imagefile", 
             fileName: "image.jpg", 
             mimeType: "image/jpeg") 
       for (key, value) in parameters { 
        multipartFormData.append(value.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key) 
       } 
     }, 
      to: completeUrl, 
      headers: headers, 
      encodingCompletion: { encodingResult in 
       switch encodingResult { 
       case .success(let upload, _, _): 
        upload.responseJSON{ response in 
         print(response) 
        } 
       case .failure(let encodingError): 
        print(encodingError) 
       } 
      } 
     ) 

Postman Response 成功上邮递员,但有问题页眉,你可以指出愚蠢的错误,我有做

+0

“代码无法运行”...您是什么意思的“不运行”?您是否收到错误,如果有,错误是什么?如果它根本没有运行,你确定你到达'responseJSON'行吗?设置中断点,看看你到哪里,以及你没有去哪里... – Rob

+0

此外,作为一种工具,像[Charles](http://charlesproxy.com)和[WireShark](http: //wireshark.org)对于协调代码的做法非常有用,而不是postman报告的内容。 – Rob

+0

无关,您通常不必手动设置“Accept”标头。阿拉莫菲尔为你做到这一点。 – Rob

我相信你需要像这样

multipartFormData.append(imageData!, 
    withName: "profile_image", 
    fileName: "image.jpg", 
    mimeType: "image/jpeg") 
追加

而且你也不需要设置或包含parameters = ["profile_image": "image file"]

如前所述@rob,用查尔斯和比较来自邮差从模拟器请求时请求。这将有助于查明有什么不同

+0

谢谢,这是由于我自己的无知,设置** withName **解决了我的问题。谢谢 –

+0

很高兴帮助 - 你能接受我的答案吗? – dmorrow