I have a .html file, size is 39.7 KB (40,687 bytes). I want a method in which when I open .html file in browser, an alert box appears with size, I don't want to use upload method, A method that will tell the size directly without doing any thing.
Asked
Active
Viewed 31 times
0
-
Does this answer your question? [Limit the size of a file upload (html input element)](https://stackoverflow.com/questions/5697605/limit-the-size-of-a-file-upload-html-input-element) – gurisko Oct 18 '20 at 09:47
-
Not without making a fetch/ajax request to server to get the size. What is your use case for this? – charlietfl Oct 18 '20 at 10:05
-
I have to make a .html file for my office. Actually I want if someone change or remove my JS code, JavaScript will detect it through file size. – Muhammad Haris Oct 18 '20 at 11:22
-
1So if someone changes your js, what's to stop them just changing the size from 40,687 to 51,876 (eg) so your check passes? – freedomn-m Oct 18 '20 at 13:11
-
1Does this answer your question? [Get file size before uploading](https://stackoverflow.com/questions/7497404/get-file-size-before-uploading) – NIKHIL NEDIYODATH Oct 18 '20 at 17:27
1 Answers
0
you can do it same as follow. when you select the file you can get the size of selected file with MB.
$("#my-file-uploader").on("change", function (e) {
var file = e.currentTarget.files[0];
var filesize = ((file.size/1024)/1024).toFixed(4); // MB
$("#file-size").html(filesize);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="my-file-uploader"/>
<div id="file-size"></div>
Mehrzad Tejareh
- 611
- 4
- 18