Alamofire 4多部分请求上传进度

时间:2016-10-07 11:42:44

标签: ios swift alamofire

如何使用Alamofire 4跟踪我的分段上传请求的进度?

我的encodingCompletion处理程序:

encodingCompletion: {
        encodingResult in
        switch encodingResult {
        case .success(let uploadRequest, _, _):
            uploadRequest.uploadProgress {
                p in
                print(p.completedUnitCount, p.totalUnitCount)
            }
            break
        case .failure( _):
            print("Failed to encode upload")
        }
}

我得到的错误说:

  

无法调用非功能类型的值'进度'

2 个答案:

答案 0 :(得分:17)

试试这个:

Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(URL(string: "http://example.com/url1")!, withName: "one")
            multipartFormData.append(URL(string: "http://example.com/url2")!, withName: "two")
        },
        to: "http://example.com/to",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
                upload.uploadProgress { progress in

                    print(progress.fractionCompleted)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

答案 1 :(得分:1)

你需要将fractionCompletedtotalUnitCountcompletedUnitCount包含一个强制转换为Int或Float(取决于你需要的东西)。

有效!

来源:https://github.com/Alamofire/Alamofire/issues/1652#issuecomment-259020449