当使用Alamofire.upload方法与Alamofire 4.0.0时,错误返回,我找不到解决方案

时间:2016-12-22 17:56:48

标签: xcode swift3 alamofire

<style name="your_text_style">
    <item name="android:fontFeatureSettings">liga 0</item>
</style>

当我运行上面的代码时,错误&#34;对成员的不明确的引用&#39;(_:to:method:headers :)&#34;出现,我不知道解决方案是什么。请帮忙。感谢。

2 个答案:

答案 0 :(得分:1)

您需要参数为url并删除方法名称。

Alamofire.upload(
multipartFormData: {multipartFormData in
                multipartFormData.appendBodyPart(data: imgData, name: "fileupload", fileName:"image", mimeType: "image/jpg")
                multipartFormData.appendBodyPart(data: keyData, name: "key")
                multipartFormData.appendBodyPart(data: keyJSON, name: "format")
            },
to: "https://httpbin.org/post", //URL,
encodingCompletion: { encodingResult in
    switch encodingResult {
    case .success(let upload, _, _):
        upload.responseJSON { response in
            debugPrint(response)
        }
        upload.uploadProgress(closure: { //Get Progress
                progress in
                    print(progress.fractionCompleted)
            })
    case .failure(let encodingError):
        print(encodingError)
    }
})

答案 1 :(得分:0)

您使用的是较旧的upload语法。它应该是:

if let img = ImageSelectorImg.image, imageSelected {
    let urlStr = "https://post.imageshack.us/upload_api.php"
    let url = URL(string: urlStr)!
    let imgData = UIImageJPEGRepresentation(img, 0.2)!
    let keyData = "AEHK...d66b".data(using: .utf8)!  // key removed for sake of privacy
    let keyJSON = "json".data(using: .utf8)!

    Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imgData, withName: "fileupload", fileName:"image", mimeType: "image/jpg")
        multipartFormData.append(keyData, withName: "key")
        multipartFormData.append(keyJSON, withName: "format")
    }, to: url, encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                if let info = response.result.value as? [String: AnyObject],
                    let links = info["links"] as? [String: AnyObject],
                    let imgLink = links["image_link"] as? String {
                            self.postToFirebase(imgLink)
                }
            }
        case.failure(_): break
        }
    })
}

注意,method参数默认为.post,因此您不必提供该参数。