1

I want to get the size of a file from this path:

my-path/filename

How can I get the size of this file?

4
  • Is that a file path or a URL? It looks like a URL with the schema missing, but the answer will be very different if it is one. Commented Feb 7, 2023 at 15:10
  • 1
    Why are you marking the file path / URL as a quote? Who are you quoting? Please take care to format your questions correctly. Commented Feb 7, 2023 at 15:10
  • You'll need to make a request. If the server reports back a correct Content-Length header, a HEAD request will probably do; otherwise you'll have to request the full thing. Commented Feb 7, 2023 at 15:10
  • Crazy I can not find a dupe with an accepted answer Commented Feb 7, 2023 at 15:19

1 Answer 1

2

Use the fetch() method and only set the request method to HEAD.

    const fetchResponse = await fetch(src, { method: 'HEAD' });
    const size = fetchResponse.headers.get('content-length');

Beware, it will return a number represented as a string.

Sign up to request clarification or add additional context in comments.

2 Comments

you can auto convert to a number by putting a + before fetchResponse. const size = +fetchResponse.headers.get('content-length');
but when using the + be aware of values like null, true, false and such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.