下载 Google Drive 直接链接时如何显示正确的进度?

dja*_*ler 2 dart flutter

我有一个来自 Google Drive 的直接链接,我的应用程序将下载并显示百分比,它可以正确下载任何其他链接,但只有使用 Google Drive 直接链接,它才会显示一个很大的负数。

“Baixando”在葡萄牙语中的意思是“下载”。

应用程序屏幕

  await dio.download(
    widget.document['url'],
    path,
    onReceiveProgress: (received, total){

      setState(() {
        progress = "Baixando..."+((received / total) * 100).toStringAsFixed(0) + "%";
      });

  });
Run Code Online (Sandbox Code Playgroud)

例如,使用此 Google Drive 直接链接 (pdf):https : //drive.google.com/uc?export=download&id=1N8D8lx1HlW0X99wovGEQxZRUtewN6-J2

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

Mar*_*lle 5

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

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) + "%");
  }
});
Run Code Online (Sandbox Code Playgroud)

它还指出:

接收数据时:如果事先不知道响应体的大小,total 将为-1,例如:响应数据使用 gzip 压缩或没有 content-length 标头。

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

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