最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to display the size of a file without downloading it? - Stack Overflow

programmeradmin6浏览0评论

I can't seem to find a way to display the size of a file in JavaScript in a terminal simulator. (I'm very new to JavaScript)

I've tried these:

Ajax - Get size of file before downloading

My expected results were to get the byte size but nothing happens. I'm not able to show any error messages (if there were any) as I am on my school laptop and they blocked Inspect Element.

The output needs to be displayed on the "terminal" itself and it must be strictly JavaScript.

Thanks!

Edit 1:

These are the "terminal" files to make it easier than making files based on snippets that are the whole source. The mands are located at js/terminal.html. The main area we need to pay attention to is Line 144.

I would post it in snippets but I'd make this question 20x the size it is. It's based on Andrew Barfield's HTML5 Terminal

I can't seem to find a way to display the size of a file in JavaScript in a terminal simulator. (I'm very new to JavaScript)

I've tried these:

https://bitexperts./Question/Detail/3316/determine-file-size-in-javascript-without-downloading-a-file

Ajax - Get size of file before downloading

My expected results were to get the byte size but nothing happens. I'm not able to show any error messages (if there were any) as I am on my school laptop and they blocked Inspect Element.

The output needs to be displayed on the "terminal" itself and it must be strictly JavaScript.

Thanks!

Edit 1:

These are the "terminal" files to make it easier than making files based on snippets that are the whole source. The mands are located at js/terminal.html. The main area we need to pay attention to is Line 144.

I would post it in snippets but I'd make this question 20x the size it is. It's based on Andrew Barfield's HTML5 Terminal

Share Improve this question edited May 15, 2019 at 13:00 enthusiasticGeek asked May 14, 2019 at 19:06 enthusiasticGeekenthusiasticGeek 491 silver badge6 bronze badges 4
  • 2 Is this file held on the server? If so then you would need to write some server side logic to retrieve the file size and return that to your JS. – Rory McCrossan Commented May 14, 2019 at 19:07
  • 2 If the server implements developer.mozilla/en-US/docs/Web/HTTP/Methods/HEAD you can use it. You need a better explanation of what you tried and what nothing happens means. – Ruan Mendes Commented May 14, 2019 at 19:09
  • Have they blocked the F12 key entirely? If not the network tab will show you the AJAX request and response. – Dave S Commented May 14, 2019 at 19:15
  • 1 Trying to learn web coding without dev tools will really slow you down. You're better off getting back to it when you can test your code – Ruan Mendes Commented May 14, 2019 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 9

If the server supports HEAD, you can try to use that. However, there's no guarantee that the Content-Length header is returned even if HEAD requests are supported!

Run the below code in a console from stackoverflow and you'll see the size of HTML for their home page without downloading the full page. (Note that StackOverflow no longer provides a content-length header)

fetch('/', {method: 'HEAD'}).then((result) => {
   console.log(result.headers.get("content-length"))
})

https://developer.mozilla/en-US/docs/Web/HTTP/Methods/HEAD

The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method.

发布评论

评论列表(0)

  1. 暂无评论