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

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

问题描述

我正在迅速开发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-data Alamofire.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的问题,第一个没有使用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.

任何人都可以请

谢谢! :)

推荐答案

您可以在代码下方使用 Alamofire 3.0 +

you can use Alamofire 3.0+ below code

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天全站免登陆