2017-01-16 101 views
0

迁移对我来说简直就是一场噩梦。我从以前的版本雨燕/的iOS/Alamofire如何在Alamofire4中进行multipartFormData上传?

let intVal = 0 

Alamofire.upload(.POST, url, headers: ["StringValue": intVal, "StringValue2": "StringValue3"], multipartFormData: { mpfd in 
     let image = self.profileImageView.image! 
     let imageData = UIImageJPEGRepresentation(image, 0.8)! 
     mpfd.appendBodyPart(data: imageData, name: "image", fileName: "custom_image.jpg", mimeType: "image/jpeg") 
     }, encodingCompletion: { result in 

      switch result { 
      case .success(let request, _, _): 
       let response = request.response 
       print("response from image change: \(response)") 
       print("Successfully changed pro pic") 
      case .failure/*(let encodingError)*/: 
       print("Failed to change pro pic") 
      } 

    }) 

的这个老代码,但现在的Xcode是给我一个错误说“不明确提及成员“上传(_:到:方法:标题)”,但我不知道我是否可以信任这些错误消息,因为Alamofire会触发并且现在抛出数千个错误,例如encoding: .json现在是JSONEncoding.default,但是Xcode告诉我错误是“函数调用中的额外方法”。所以,我想大多数其他错误的解决方案是切换的方法和URL参数

Alamofire.upload(url, method: .post, headers ...) 

但是,这也不能正常工作。我应该如何重写这个新的Swift/Alamofire?

+0

[相关Q​​&A](http://stackoverflow.com/questions/41401913/cannot-invoke-append-with-an-argument-list- of-type-string-withname-string /) – dfri

回答

1

有一个在Alamofire的测试套件的一个示例:https://github.com/Alamofire/Alamofire/blob/9688b16f0546b97b16c775c75f42b3f4bfacc78e/Tests/UploadTests.swift#L244

guard let image = self.profileImageView.image, 
    let imageData = UIImageJPEGRepresentation(image, 0.8) else { 
    return 
} 

Alamofire.upload(
    multipartFormData: { multipartFormData in 
    mpfd.append(imageData, withName: "image", fileName: "custom_image.jpg", mimeType: "image/jpeg") 
    }, 
    to: url, 
    headers: ["Header": value, "Another_Header": value2], 
    encodingCompletion: { result in 
    // Whatever 
    } 
) 
+0

我会在哪里放一个'headers'参数,就像我原来的那样?这实际上是我的主要问题 –

+0

这是'upload'函数的可选参数。见https://github.com/Alamofire/Alamofire/blob/2b65bfe6608a236772b4151b0ec9e82a51a22131/Source/Alamofire.swift#L376 - 我编辑了包含标题的答案 – Estel