Get file size without downloading it

Viewed 3234

How to get file size from URL (in Flutter)? I am able to get it by using:

http.Response response = await http.get(url);
print(response.contentLength);

But that downloads the entire file. Is it possible to get the file size without entirely downloading it? Thanks

2 Answers

Found the answer: HEAD Request.

http.Response r = await http.head(url);
r.headers["content-length"]

Note: r.contentLength; directly doesn't work.

I tried the above-accepted answer for firebase storage file download size before download the file. But I see that http.head doesn't contain content-length. So I tried http.get method and I saw the content-length is contained in the header.So This should be the correct answer I think.

http.Response r = await http.get(url);
final file_size = r.headers["content-length"];
Related