swift - 获取分段上传 Alamofire5 的上传进度

标签 swift alamofire alamofire5

在 Alamofire5 之前,我们可以使用 uploadRequest 的 encodingReresult 得到 uploadProgress .
但是现在在将 Alamofire 上传到版本 5 之后,基于 Alamofire Documentation ,我们可以使用 .uploadProgress为了获得上传进度处理程序。

这是我的代码:

AF.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(fileContent, withName: "file", fileName: filePath.lastPathComponent)
            multipartFormData.append(token.data(using: .utf8)!, withName: "token")
        }, to: uploadURL)
        .uploadProgress { progress in 
            print(progress)
        }
        .responseJSON { [weak self] response in
            print(response)
        }

但是uploadProgress在上传过程中从未调用关闭。

我查了很多 stackoverflow问题,但没有人工作。

最佳答案

更换你的

.uploadProgress { progress in 
            print(progress)
        }


.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})

它会给你输出:
Upload Progress: 0.035203331252732804
Upload Progress: 0.035203331252732804
Upload Progress: 0.0528049968790992
Upload Progress: 0.088008328131832
Upload Progress: 0.1584149906372976
Upload Progress: 0.2112199875163968
Upload Progress: 0.2288216531427632
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962

编辑 :
AF.upload(multipartFormData: { MultipartFormData in
        MultipartFormData.append(fileContent, withName: "file" , fileName: filePath.lastPathComponent , mimeType: "image/jpeg")
        for(key,value) in dictonary {
            MultipartFormData.append(token.data(using: String.Encoding.utf8)!, withName: "token")
        }
    }, to: uploadURL, method: .post, headers: ["Content-Type": "application/json")

        .uploadProgress(closure: { (progress) in
            print("Upload Progress: \(progress.fractionCompleted)")
        })

        .responseJSON{ (response) in
            debugPrint("SUCCESS RESPONSE: \(response)")
         }

关于swift - 获取分段上传 Alamofire5 的上传进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62383841/

相关文章:

swift - 当我创建 JSON 输出时,Alamofire 的 Optional({}) 是什么意思

ios - Alamofire 5 委托(delegate)中缺少 sessionDidReceiveChallenge

swift - AlamoFire downloadProgress 完成处理程序异步/等待

ios - 如何检查 UITextField 何时更改?

ios - 如何在不修改其余约束的情况下在 View Controller 上插入元素?

swift - 从 SPM 中 Xcode 生成的项目出现运行时错误(dyld : Library not loaded, 不兼容的库版本)

ios - 如何创建可从 View Controller 访问的函数?

ios - Alamofire:遵循 HTTP 重定向(或不遵循)

swift - 列出我的 YouTube 播放列表时出错