简体   繁体   中英

Alamofire multipartFormData upload doesn't send data

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()
            }
        }
    )

经过近4个小时的研究,我发现API也需要文件名,这里是我改变的行,一切正常。

multipartFormData.appendBodyPart(data: imageData, name: ParameterKey.Avatar, fileName: "avatar.jpg", mimeType: "image/jpeg")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM