Swift 3 Alamofire分段上传 [英] Swift 3 Alamofire multipart upload

查看:451
本文介绍了Swift 3 Alamofire分段上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢迁移到Swift 3,我发现很难编译使用Alamofire的项目。

Thanks to migration to Swift 3, I find it difficult to compile my project that uses Alamofire.

上传multipartFormData时出现问题:

The problem occurs when uploading multipartFormData:

Alamofire.upload(.POST, URL, headers: headers, multipartFormData: {
        multipartFormData in
.
.
. 
}) 




模棱两可的参考会员'上传(_:to:方法:标题:)'

Ambiguous reference to member 'upload(_:to:method:headers:)'

任何帮助非常感谢,提前感谢!

Any help much appreciated, thanks in advance!

已解决:

 Alamofire.upload(multipartFormData: { (multipartFormData) in

        multipartFormData.append(fileData, withName: "file_pack", fileName: "file_pack", mimeType: "text/plain")


        for (key, value) in self.parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
        }, with: URL2, encodingCompletion: { (result) in

            switch result {
            case .success(let upload, _, _):

                upload.responseJSON { response in
                    self.delegate?.showSuccessAlert()
                    print(response.request)  // original URL request
                    print(response.response) // URL response
                    print(response.data)     // server data
                    print(response.result)   // result of response serialization
                    //                        self.showSuccesAlert()
                    self.removeImage("frame", fileExtension: "txt")
                    if let JSON = response.result.value {
                        print("JSON: \(JSON)")
                    }
                }

            case .failure(let encodingError):
                self.delegate?.showFailAlert()
                print(encodingError)
            }

    })

这是在Swift中实现上传方法的方法3

This is how upload method should be implemented in Swift 3

推荐答案

例如,在< Alamofire 4.0.0 Swift 3

(确保你已准备好4.0.0,因为你好像还没有更新你的Alamofire)

(make sure you are 4.0.0 ready as it looks like you haven't updated your Alamofire yet)

Alamofire.upload(multipartFormData: { (multipartFormData) in
        // code
    }, to: URL, encodingCompletion: { (result) in
        // code
    })

Alamofire.upload(multipartFormData: { (multipartFormData) in
        // code
    }, with: URL, encodingCompletion: { (result) in
        // code
    })

所以标题需要通过网址请求传递:

So headers need to be passed by URL request:

let URL = try! URLRequest(url: "http://example.com", method: .get, headers: headers)

这篇关于Swift 3 Alamofire分段上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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