使用Alamofire和multipart / form-data [英] Using Alamofire and multipart/form-data

查看:632
本文介绍了使用Alamofire和multipart / form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法以正确的方式接近我提供的API,以便为我提供我正在寻找的响应。我一直在使用Swift和Alamofire,但这是我第一次使用multipart / form-data上传图像。我可以使用Postman上传图像,但我无法通过我的应用程序使用Alamofire框架发送相同的消息。

I'm unable to approach the API that has been offered to me in the proper way for it to give me the response I'm looking for. I've been using Swift and Alamofire for a while but this is the first time I've to upload images using multipart/form-data. I'm able to upload images using Postman but I'm unable to get the same message send out by my application using the Alamofire framework.

我的Swift代码:

func postFulfilWish(wish_id: Int, picture : UIImage, completionHandler: ((AnyObject?, ErrorType?) -> Void)) {

    var urlPostFulfilWish = Constant.apiUrl;
    urlPostFulfilWish += "/wishes/";
    urlPostFulfilWish += String(wish_id);
    urlPostFulfilWish += "/fulfill/images"  ;

    let image : NSData = UIImagePNGRepresentation(UIImage(named: "location.png")!)!

    Alamofire.upload(.POST, urlPostFulfilWish, headers: Constant.headers, multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(data: image, name: "file")
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { response in
                    //This is where the code ends up now
                    //So it's able to encode my message into multipart/form-data but it's not doing it in the correct way for the API to handle it
                    debugPrint(response)
                }
            case .Failure(let encodingError):
                print(encodingError)
            }
        }
    )
}


推荐答案

如果还没有回答,最近我使用Alamofire遇到同样的问题Ť o使用表单数据上传图片。

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

我能够使用Postman完全按照此帖子中显示的方式上传图片,但无法使用Alamofire我的应用程序。

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.

您需要检查两件事,首先是服务器所期望的文件名,第二件是用于附加正文部分的方法在multipartFormData闭包中。

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")

这个既不是

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")

问题基本上是服务器无法找到fi具有预期名称的le。

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.

这篇关于使用Alamofire和multipart / form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆