21

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

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

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

Alamofire.upload(.POST, uploadURL , fileURL)

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

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

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

任何人都可以帮我解决这个问题吗?

谢谢!:)

4

6 回答 6

25

您可以在下面的代码中使用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)
        }
    })
}
于 2016-02-16T17:16:20.427 回答
4

SWIFT 2 AlamoFire 简单图像上传(REST API)

@amit gupta 似乎答案包含很大的开销。AlamoFire 包含大量简化的解决方案。Alamofire.request 方法包含几个简化的重载,可以用来以简单的方式上传。通过使用 Alamofire.request( 方法,开发人员可以摆脱编码开销。

由于未指定正确的媒体类型,HTTP 状态 415 给出。

请在下面查看我的解决方案。

import UIKit
import Alamofire

class ViewController: UIViewController {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var btnUpload: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func successDataHandler(responseData:String){

        print ("IMAGE UPLOAD SUCCESSFUL    !!!")

    }

    func failureDataHandler(errorData:String){

        print ("  !!!   IMAGE UPLOAD FAILURE   !!! ")

    }

    @IBAction func actionUpload(sender: AnyObject) {

        let URL = "http://m8coreapibeta.azurewebsites.net/api/cards/SaveImages"

        let postDataProlife:[String:AnyObject] = ["CardId":(dataCardDetail?.userId)!,"ImageType":1,"ImageData":imageView.image!]

        uplaodImageData(URL, postData: postDataProlife, successHandler: successDataHandler, failureHandler: failureDataHandler)
    }

    func uplaodImageData(RequestURL: String,postData:[String:AnyObject]?,successHandler: (String) -> (),failureHandler: (String) -> ()) -> () {

        let headerData:[String : String] = ["Content-Type":"application/json"]

        Alamofire.request(.POST,RequestURL, parameters: postData, encoding: .URLEncodedInURL, headers: headerData).responseString{ response in
            switch response.result {
            case .Success:
                print(response.response?.statusCode)
                successHandler(response.result.value!)
            case .Failure(let error):
                failureHandler("\(error)")
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
于 2016-03-19T02:11:07.500 回答
3

带有 swift 3.0 的 Almaofire

Alamofire.upload(multipartFormData: { multipartFormData in
    var index = 1
    for image in imageArray {
        let imageData: Data = (UIImageJPEGRepresentation(image, 1.0) as Data?)!

        multipartFormData.append(imageData, withName: "home-\(index)", fileName: "home-\(index)", mimeType: "image/jpeg")

        index += 1
    }
    }, with: requestName, encodingCompletion: { result in
        switch result {
        case .success(let upload, _, _):

            upload.responseJSON { response in
                print("Image(s) Uploaded successfully:\(response)")
            }
        case .failure(let encodingError):
            print("encodingError:\(encodingError)")
        }
})
于 2017-04-08T07:42:28.553 回答
3

Swift 4 与 Alamofire 4

let isConnected = connectivity.isConnectedToInternet()

  func updateProfile(firstName:String,lastName:String ,imageData:Data?,completion: @escaping (isValidUser)->()) {


    if self.isConnected {

        var parameters : [String:String] = [:]
        parameters["auth_key"] = loginUser?.authKey!
        parameters["first_name"] = firstName
        parameters["last_name"] = lastName

        let url = "\(baseUrl)\(basicAuthenticationUrl.updateProfile)"
        print(url)


        Alamofire.upload(multipartFormData: { (multipartFormData) in
            for (key, value) in parameters {
                multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
            }

            if let data = imageData {
                multipartFormData.append(data, withName: "image_url", fileName: "image.png", mimeType: "image/png")
            }

        }, usingThreshold: UInt64.init(), to: url, method: .post) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    print("Succesfully uploaded  = \(response)")
                    if let err = response.error{

                        print(err)
                        return
                    }

                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")

            }
        }
    }

}
于 2018-02-17T12:01:11.510 回答
0

带有 swift 2.0 的 Almaofire 只需复制并粘贴以下代码。这里是来自服务器的 JSON 响应

 func uploadImageRemote (imageData : NSData?) -> Dictionary <String,AnyObject>{
    var imageDictionary = Dictionary<String,AnyObject>()

      var tokenHeaders:[String:String]! = ["x-access-token":Constants.kUserDefaults.stringForKey("userToken")!]
    Alamofire.upload(
        .POST,
        "http://52.26.230.146:3300/api/profiles/imageUpload",headers:tokenHeaders,
        multipartFormData: { multipartFormData in
            multipartFormData.appendBodyPart(data: imageData!, name: "upload", fileName: "imageFileName.jpg", mimeType: "image/jpeg")
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
                    print("Uploading Avatar \(totalBytesWritten) / \(totalBytesExpectedToWrite)")
                    dispatch_async(dispatch_get_main_queue(),{

                    })
                }
                upload.responseJSON { response in
                    guard response.result.error == nil else {
                        print("error calling GET \(response.result.error!)")
                        return
                    }

                    if let value = response.result.value {
                       print("Success JSON is:\(value)")
                        if let result = value as? Dictionary<String, AnyObject> {
                            imageDictionary["imageUrl"] = result["url"]
                        }
                    }

                    dispatch_async(dispatch_get_main_queue(),{
                        //Show Alert in UI
                        print("Avatar uploaded");
                    })
                }

            case .Failure(let encodingError):
                //Show Alert in UI
                print("Avatar not uploaded \(encodingError)");
            }
        }
    );



return imageDictionary
}
于 2016-07-28T10:36:25.143 回答
-2

要使用编码参数,请创建一个 ParameterEncoding 变量,为其分配一个编码类型(包括 .JSON、.URL 的枚举的情况),然后将编码函数与您的 NSURLRequest 和参数一起使用。这个函数返回一个包含两个元素的元组,第一个是产生的 NSURLRequest,第二个是产生的可能的 NSError。

这是我如何将它用于项目中需要的自定义标题

 var parameterEncoding:ParameterEncoding!
    switch method {
        case .POST, .PUT :
            parameterEncoding = ParameterEncoding.JSON
        default :
            parameterEncoding = ParameterEncoding.URL
    }
    var alamoRequest = Alamofire.Manager.sharedInstance.request(parameterEncoding.encode(mutableURLRequest, parameters: parameters).0)
于 2014-10-24T17:05:24.677 回答