小编典典

Swift 3 Alamofire分段上传

swift

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

上载multipartFormData时发生问题:

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

对成员’upload(_:to:method:headers :)的含糊引用

任何帮助,不胜感激,在此先感谢!

解决:

 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中实现上传方法的方式


阅读 403

收藏
2020-07-07

共1个答案

小编典典

例如,在 Swift 3中* 使用 Alamofire 4.0.0*

(确保您已经准备好4.0.0,因为您似乎尚未更新Alamofire)

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

要么

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

因此headers需要通过URL请求传递:

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