首页 > 解决方案 > 下载 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 直接链接时它不会改变。

标签: dartflutter

解决方案


我检查了包的文档,这是示例代码:

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 链接显示此行为的原因是缺少内容长度标头或文件压缩。您可能需要找到从云端硬盘下载文件的替代方法。


推荐阅读