Reputation: 402
I'm trying to upload an image using Alamofire but the server is not getting the image. This is the code where I make the upload:
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()
}
}
)
Upvotes: 4
Views: 1892
Reputation: 402
After almost 4 hours looking into this I found out that the API needed the file name too, here's the line I changed and everything worked.
multipartFormData.appendBodyPart(data: imageData, name: ParameterKey.Avatar, fileName: "avatar.jpg", mimeType: "image/jpeg")
Upvotes: 4