Alamofire 5上传encodingCompletion

时间:2019-03-04 14:12:24

标签: swift alamofire

我正在使用Swift 4和Alamofire 5,我上传了两张多框照片,并且要打印进度

 AF.upload(
        multipartFormData: { MultipartFormData in

            MultipartFormData.append(firstPic, withName: "first_pic", fileName: "image.jpeg", mimeType: "image/jpeg")
            MultipartFormData.append(secondPic, withName: "second_pic", fileName: "image.jpeg", mimeType: "image/jpeg")

    }, to: urlString, encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                print(totalBytesRead)
            }
            upload.responseJSON { request, response, result in
                print(result)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })

并显示一条错误消息

参数标签'(multipartFormData :, to :, encodingCompletion :)'与任何可用的重载都不匹配

库是否更新了代码或其他内容?

3 个答案:

答案 0 :(得分:2)

Alamofire 5不再需要encodingCompletion!取而代之的是,多部分表单编码是标准的现在异步请求过程的一部分,它将在Request上返回错误,并且可以在validateresponse*调用期间使用它们。

答案 1 :(得分:0)

请根据您的需要进行修改

func upload(image: Data, to url: Alamofire.URLRequestConvertible, params: [String: Any]) {
    AF.upload(multipartFormData: { multiPart in
        for (key, value) in params {
            if let temp = value as? String {
                multiPart.append(temp.data(using: .utf8)!, withName: key)
            }
            if let temp = value as? Int {
                multiPart.append("\(temp)".data(using: .utf8)!, withName: key)
            }
            if let temp = value as? NSArray {
                temp.forEach({ element in
                    let keyObj = key + "[]"
                    if let string = element as? String {
                        multiPart.append(string.data(using: .utf8)!, withName: keyObj)
                    } else
                        if let num = element as? Int {
                            let value = "\(num)"
                            multiPart.append(value.data(using: .utf8)!, withName: keyObj)
                    }
                })
            }
        }
        multiPart.append(image, withName: "file", fileName: "file.png", mimeType: "image/png")
    }, with: url)
        .uploadProgress(queue: .main, closure: { progress in
            //Current upload progress of file 
            print("Upload Progress: \(progress.fractionCompleted)")
        })
        .responseJSON(completionHandler: { data in
            //Do what ever you want to do with response
        })
}

答案 2 :(得分:0)

let headers: HTTPHeaders = [
            /* "Authorization": "your_access_token",  in case you need authorization header */
            "Content-type": "multipart/form-data"
        ]


            AF.upload(
                multipartFormData: { multipartFormData in
                    multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "file.jpeg", mimeType: "image/jpeg")
            },
                to: "http://----/new.php", method: .post , headers: headers)
                .response { resp in
                    print(resp)


            }