如何在 Alamofire 4 中进行多部分表单数据上传?
How to do a multipartFormData upload in Alamofire4?
迁移对我来说简直就是一场噩梦。我有以前版本 Swift/iOS/Alamofire
的旧代码
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 给我一个错误说 "Ambiguous reference to member 'upload(_:to:method:headers)" 但我不知道我是否可以相信这些错误消息,因为 Alamofire 调用并现在抛出数千个错误,一个例子是encoding: .json
现在是 JSONEncoding.default
,但是 Xcode 告诉我错误是 "Extra method in function call"。所以我尝试了大多数其他错误的解决方案,即切换方法和 url 参数
Alamofire.upload(url, method: .post, headers ...)
但这也行不通。我应该如何重写它以使用新的 Swift/Alamofire?
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
}
)
迁移对我来说简直就是一场噩梦。我有以前版本 Swift/iOS/Alamofire
的旧代码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 给我一个错误说 "Ambiguous reference to member 'upload(_:to:method:headers)" 但我不知道我是否可以相信这些错误消息,因为 Alamofire 调用并现在抛出数千个错误,一个例子是encoding: .json
现在是 JSONEncoding.default
,但是 Xcode 告诉我错误是 "Extra method in function call"。所以我尝试了大多数其他错误的解决方案,即切换方法和 url 参数
Alamofire.upload(url, method: .post, headers ...)
但这也行不通。我应该如何重写它以使用新的 Swift/Alamofire?
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
}
)