Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting upload file size before upload

When the user selects a file to be uploaded, is there a way I can get the exact size of this file before the upload even begins? I'm guessing this needs to be done on the client side with jQuery or JavaScript. Any ideas how?

like image 406
sami Avatar asked Nov 16 '10 03:11

sami


People also ask

How can get upload file size in PHP?

The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.

How can I see how big a file will upload before Outsystems?

For Traditional Web you could indeed use the BinaryDataSize action from the BinaryData API. For Reactive Web, if you want to check the file size before uploading the file to the server, you would need to check the file size at the client side.

How do I limit the upload file size in HTML?

size > 2097152){ alert("File is too big!"); this. value = ""; }; }; This example should work fine. I set it up for roughly 2MB, 1MB in Bytes is 1,048,576 so you can multiply it by the limit you need.


2 Answers

This cannot be done in pure Javascript in current browsers.

Instead, you can use Uploadify, which uses Flash.

In non-IE browsers, you can also use HTML5 to read files on the client.

like image 197
SLaks Avatar answered Oct 13 '22 01:10

SLaks


$("#file_input_selector").bind('change', function(){
 alert(this.files[0].size);
});

Not sure of all the compatibility issues, but this seems to be working just fine for me.

like image 40
Freer Avatar answered Oct 13 '22 01:10

Freer