跟踪Alamofire请求的进度

时间:2019-10-18 08:57:36

标签: ios swift alamofire

我想知道可以显示请求进度。我将图像转换为base64字符串,并使用参数将其发送到服务器。有没有办法追踪进度?我想尝试类似的方法。但是我无法在Alamofire.request中添加进度部分。有什么我想念的吗?

Alamofire.request(.POST, URL, parameters: parameter, encoding: .JSON)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
 // track progress here
 }
.responseJSON { response in
// Do your stuff
}

2 个答案:

答案 0 :(得分:0)

不确定,但是我认为当前版本的Alamofire使用downloadProgress而不是progress

Alamofire.request(/* ... */).downloadProgress { progress in
  progress.fractionCompleted // value between 0 and 1
}
.responseJSON { /* ... */ }

答案 1 :(得分:0)

您可以这样做

        Alamofire.upload(multipartFormData: { (multipartFormData) in





        }, with: URL, encodingCompletion: { (result) in

            switch result {

            case .success(let upload, _, _):



                upload.uploadProgress(closure: { (Progress) in



                    // Here you get the progress

                    print(Progress.fractionCompleted)




                })


                upload.responseJSON { response in






            case .failure( _ ):



            }

        })