dart - 下载 Google Drive 直接链接时如何显示正确的进度?
问题描述
我有一个来自 Google Drive 的直接链接,我的应用程序将下载并显示百分比,它正确下载的任何其他链接,但只有 Google Drive 直接链接显示一个很大的负数。
“Baixando”在葡萄牙语中的意思是“下载”。
await dio.download(
widget.document['url'],
path,
onReceiveProgress: (received, total){
setState(() {
progress = "Baixando..."+((received / total) * 100).toStringAsFixed(0) + "%";
});
});
例如,使用此 Google Drive 直接链接 (pdf):https ://drive.google.com/uc?export=download&id=1N8D8lx1HlW0X99wovGEQxZRUtewN6-J2
更新:“收到”以字节为单位获取文件的大小,“总计”我只得到值:-1,尝试下载 Google Drive 直接链接时它不会改变。
解决方案
我检查了包的文档,这是示例代码:
await dio.download(url, "./example/flutter.svg",
options: Options(headers: {HttpHeaders.acceptEncodingHeader: "*"}), // disable gzip
onProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + "%");
}
});
它还指出:
接收数据时:如果事先不知道响应体的大小,total 将为-1,例如:响应数据用 gzip 压缩或没有 content-length 头。
您没有在您的方法中执行此检查,因此当正在下载的文件的总大小未知时,您总是会得到那个大的负数。
只有 Google Drive 链接显示此行为的原因是缺少内容长度标头或文件压缩。您可能需要找到从云端硬盘下载文件的替代方法。
推荐阅读
- ios - 当 isSecureTextEntry 属性设置为 true 时,UITextField 内容宽度错误
- eclipse - 在 Ubuntu 上从 Eclipse Mars 升级到 Photon 会导致问题
- javascript - Selenium:需要脚本来使用键盘向上或向下移动幻灯片,并且需要单击并按住元素
- c# - 分组 Linq 并将列转换为行
- c++ - 使用整数减法来制作线性线渐变
- python - 什么可能导致在张量上运行 .eval() 永远不会结束?
- javascript - 如何让单个 WMS 瓦片覆盖整个谷歌地图?
- javascript - 在语言面板中切换图像(标志)
- java - 在 sqoop 导入中处理中文/日文字符
- spring-cloud - Feign客户端并发问题