Using Alamofire and multipart/form-data

痴心易碎 提交于 2019-12-01 08:26:26

In case it is not already answered, recently I had the same problem using Alamofire to upload an Image using form-data.

I was able to upload the image using Postman exactly as it's shown in this post, but no able to do it using Alamofire in my app.

You need to check two things, first of all, the name of the file that the server is expecting, and second the method used to append the body part in the multipartFormData closure.

This two methods were not working in my case -

multipartFormData.appendBodyPart(data: imageData, name: "file")

this one neither

multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: name)

But on this one worked splendid -

multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.jpeg", mimeType: "image/jpeg")

The problem basically is that the server can't find the file with the expected name.

I hope this help someone to save time wondering why it's not working.

I recently got a 404 from the server when posting a multipart request along with parameters in the body. I was using a UIImagePickerController (the delegate for which returns a UIImage) and I then sent up the PNG representation of it.

This only occurred for files that were JPEG on disk. Strangely this issue seems to only affect multipart requests that also had parameters in the body. It worked fine when the API endpoint didn't require anything else.

My guess is that there is something weird going on along the line of JPEG -> UIImage -> PNG representation that results in some sort of problem which oddly only seems to manifest itself in multipart requests that also have parameters in the body. Might be some special characters in there that makes the server not recognise the request and just return a 404.

I ended up fixing it by sending up the UIImageJPEGRepresentation of the selected image instead of UIImagePNGRepresentation, and no such errors.

You are doing debugPrint(response). You presumably should do another switch response.result { ... } and see if you got .Success or .Failure as the result of the request, and if success, you'd look at the response object contents (or if failure, look at the failure error). You need to look at that result to diagnose whether it was successful or not.

Alamofire.upload(.POST, urlPostFulfilWish, headers: Constant.headers, multipartFormData: { multipartFormData in
    multipartFormData.appendBodyPart(data: image, name: "file")
}) { encodingResult in
    switch encodingResult {
    case .Success(let upload, _, _):
        upload.responseJSON { response in
            switch response.result {
            case .Success(let value):
                print(value)
            case .Failure(let error):
                print(error)
            }
        }
    case .Failure(let encodingError):
        print(encodingError)
    }
}

I believe the question is outdated already but for as long as there is no answer accepted try the following:

multipartFormData.appendBodyPart(data: imageData, name: "name", fileName: "filename", mimeType: mimeType)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!