Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the correct progress when downloading a Google Drive direct link?

Tags:

flutter

dart

I have a direct link from Google Drive and my app will download and show the percentage, any other link it downloads correctly but only with Google Drive direct link it shows a large negative number.

"Baixando" means "Downloading" in portuguese.

app screen

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

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

  });

For example, with this Google Drive direct link (pdf): https://drive.google.com/uc?export=download&id=1N8D8lx1HlW0X99wovGEQxZRUtewN6-J2

Update: The "received" gets the size of the file in bytes, and "total" I only get the value: -1, it doesn't change when trying to download Google Drive direct links.

like image 362
djalmafreestyler Avatar asked Oct 24 '25 15:10

djalmafreestyler


1 Answers

I checked the docs for the package and this is the example code:

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) + "%");
  }
});

It also states:

When receiving data: total will be -1 if the size of the response body is not known in advance, for example: response data is compressed with gzip or no content-length header.

You're not performing this check in your method, thus you'll always get that big negative number when the total size of the file being downloaded is not known.

The reason why only Google Drive links show this behavior is the absence of the content-length header or the compression of the files. You might need to find and alternative way of downloading files from Drive.

like image 180
Mariano Uvalle Avatar answered Oct 27 '25 04:10

Mariano Uvalle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!