我能够使用 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/