如何在 Swift 中使用 Alamofire 上传带参数的图像 [英] How to upload image with parameters using Alamofire in Swift

查看:81
本文介绍了如何在 Swift 中使用 Alamofire 上传带参数的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 swift 开发一个 iPhone 应用程序.我正在使用 Alamofire 框架 来处理 http 请求.我将 Alamofire.request 用于 POST 、 GET 等,如下所示:

I am developing an iPhone application with swift. and I'am using Alamofire framework for handling http requests. I use Alamofire.request for POST , GET and etc like this:

Alamofire.request(.POST, myURL , parameters: ["a": "1", "b" : "2" ])
        .response { (request, response, data, error) in
}  

我使用 Alamofire.upload 将图像上传到服务器:

And I use Alamofire.upload to upload image to server this :

Alamofire.upload(.POST, uploadURL , fileURL)

两者都完美运行,但现在我想上传图像并发送一些参数,我的内容类型应该是 multipart/form-dataAlamofire.upload 不接受参数.

And both works perfectly, but now I want to upload an image and also send some parameters with, and my content type should be multipart/form-data and Alamofire.upload does not accept parameters.

关于 swift 的这个问题还有两个关于 SO 的问题,第一个 没有使用 Alamofire(真的,为什么不呢?)并且在 第二个mattt(Alamofire 开发者)引用使用编码参数.

There are two more question on SO about this issue with swift, which first one is not using Alamofire (and really, why not?) and in second one, mattt (Alamofire Developer) cited to use encoding parameters.

我查看了他的示例,但仍然无法弄清楚如何做到这一点.

I checked his example, but still couldn't figure out how to do that.

谁能帮我解决这个问题?

Can any one please help me solve this problem?

谢谢!:)

推荐答案

你可以使用Alamofire 3.0+下面的代码

func uploadImageAndData(){
    //parameters
    let gender    = "M"
    let firstName = "firstName"
    let lastName  = "lastName"
    let dob       = "11-Jan-2000"
    let aboutme   = "aboutme"
    let token     = "token"

    var parameters = [String:AnyObject]()
    parameters = ["gender":gender,
                  "firstName":firstName,
                  "dob":dob,
                  "aboutme":about,
                  "token":token,
                  "lastName":lastName]

    let URL = "http://yourserviceurl/"
    let image = UIImage(named: "image.png")

    Alamofire.upload(.POST, URL, multipartFormData: {
        multipartFormData in

        if let imageData = UIImageJPEGRepresentation(image, 0.6) {
            multipartFormData.appendBodyPart(data: imageData, name: "image", fileName: "file.png", mimeType: "image/png")
        }

        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
    }, encodingCompletion: {
        encodingResult in

        switch encodingResult {
        case .Success(let upload, _, _):
            print("s")
            upload.responseJSON { 
                response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization

                if let JSON = response.result.value {
                    print("JSON: (JSON)")
                }
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })
}

这篇关于如何在 Swift 中使用 Alamofire 上传带参数的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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