我尝试使用Alamofire上传图片,但服务器没有获取图片。 这是我上传的代码:
Alamofire.upload(Router.UpdateUserAvatar,
multipartFormData: { (multipartFormData) -> Void in
let compressionQuality: CGFloat = 0.8
guard let imageData = UIImageJPEGRepresentation(image, compressionQuality) else {
print("Unable to get JPEG representation for image \(image)")
callCompletion()
return
}
multipartFormData.appendBodyPart(data: imageData, name: ParameterKey.Avatar, mimeType: "image/jpeg")
}, encodingCompletion: { (encodingResult) -> Void in
switch encodingResult {
case .Success(request: _, streamingFromDisk: _, streamFileURL: _):
finalResult = Result.Success(self)
callCompletion()
case .Failure(let errorType):
let error = errorType as NSError
finalResult = Result.failureForError(error, data: nil)
callCompletion()
}
}
)
答案 0 :(得分:4)
经过近4个小时的研究,我发现API也需要文件名,这里是我改变的行,一切正常。
multipartFormData.appendBodyPart(data: imageData, name: ParameterKey.Avatar, fileName: "avatar.jpg", mimeType: "image/jpeg")