gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:57:21 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com