ios - 为什么 Alamofire 的上传进度在上传期间从未达到 1.0 的完成?

标签 ios swift alamofire

我能够使用 alamofire 成功上传文件。但是,我正在尝试跟踪上传进度。我发现的是,虽然上传成功,但我可以看到我的文件已成功上传到服务器,但进度跟踪器从未达到 1.0。它往往在 8.00 之间结束 -(低于 1.0)但永远不会达到 1。这会带来问题,因为我需要处理文件上传的完成。

 Alamofire.upload(
        multipartFormData: { multipartFormData in
            for(key, value) in sendParamters{
                multipartFormData.append((value.data(using: .utf8)!), withName: key)
            }

            for fileURL in arrayURLToUpload{
                print("fileURL: \(fileURL)")
                multipartFormData.append(fileURL, withName: "file[]")
            }
    },
        to: UPLOAD_URL,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }

                /**TRACK PROGRESS OF UPLOAD**/
                upload.uploadProgress { progress in
                    print(progress.fractionCompleted) // NEVER REACHES 1.0

                    var progress = progress.fractionCompleted


                }
                /***/


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

控制台:

 0.041737145652041
 0.521714320650513
 0.772137194562759

最佳答案

你只需要改变

的位置
upload.uploadProgress { progress in
                print(progress.fractionCompleted) // NEVER REACHES 1.0

  upload.responseJSON { response in
            debugPrint(response)
        }  

这是正确的用法:

Alamofire.upload(
        multipartFormData: { multipartFormData in
            for(key, value) in sendParamters{
                multipartFormData.append((value.data(using: .utf8)!), withName: key)
            }

            for fileURL in arrayURLToUpload{
                print("fileURL: \(fileURL)")
                multipartFormData.append(fileURL, withName: "file[]")
            }
    },
        to: UPLOAD_URL,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

             /**TRACK PROGRESS OF UPLOAD**/
                upload.uploadProgress { progress in
                    print(progress.fractionCompleted)
                }
                /***/

                upload.responseJSON { response in
                    debugPrint(response)
                }

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

关于ios - 为什么 Alamofire 的上传进度在上传期间从未达到 1.0 的完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52161840/

相关文章:

ios - UIImagePickerController 在 Swift 3 的图像编辑模式下更改底部栏按钮标题

ios - Facebook IOS SDK - 如何知道何时返回应用程序

ios - 为添加边框的 UIView 创建子类的步骤是什么?

ios - 如何在后台执行计数获取请求

ios - 在 Alamofire 中设置超时

ios - 跟踪 Alamofire 请求的进度

ios - Xcode 13 命令行替换 'instruments -w' 以在带有模板的真实 iOS 设备上运行

ios - 从其他 View Controller 访问 IBOutlet 按钮

ios - XCode 13.1 : Undefined symbol: __swift_FORCE_LOAD_$_XCTestSwiftSupport

json - 将 json 集合响应映射到 swift 对象类的简单方法