【问题标题】:How to show the correct progress when downloading a Google Drive direct link?下载 Google Drive 直接链接时如何显示正确的进度?
【发布时间】:2021-07-03 16:00:38
【问题描述】:

我有一个来自 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

更新:“received”获取文件的大小(以字节为单位),“total”我只获取值:-1,在尝试下载 Google Drive 直接链接时它不会改变。

【问题讨论】:

标签: dart flutter


【解决方案1】:

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

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 header。

您没有在您的方法中执行此检查,因此当正在下载的文件的总大小未知时,您总是会得到那个大的负数。

只有 Google Drive 链接显示此行为的原因是缺少 content-length 标头或文件压缩。您可能需要找到从云端硬盘下载文件的替代方法。

【讨论】:

    【解决方案2】:

    您可以使用 Google Drive API 来获取支持标头的文件链接。这样接收到的就不会是-1,因此我们可以计算出正确的进度。

    要启用 google drive API。访问这个对我有帮助的网站。 https://www.wonderplugin.com/wordpress-tutorials/how-to-apply-for-a-google-drive-api-key/

    【讨论】:

      猜你喜欢
      • 2022-10-16
      • 2013-10-13
      • 1970-01-01
      • 2016-03-02
      • 2018-02-25
      • 2020-06-21
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多