I have a form with an input type=file.
I'd like to determine the file size prior to the submission of the form and do something with it in JS.
how to do...
for to drink..
or make smeary p--
determine file size from JS
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
the file field is readable. JS can create a loadable reference to the file through the DOM, however, this asks the browser to load the file. At this point the browser could easily run out of ram if the file is "large" ... There was a perl script posted/linked-to some time ago that allowed the progress bar to be fairly accurate (based on the submission headers) I think..
I found this and it works ok...probably only works in IE and in a controlled environment...luckly for me I have both and will ONLY have both 
Code: Select all
function showSize(){
var objSize = new ActiveXObject("e;Scripting.FileSystemObject"e;);
var strFileName = objSize.getFile(document.MyForm.bob.value);
var SizeOfFile = strFileName.size;
alert(SizeOfFile + "e; bytes"e;);
return false;
}Just in case you haven't heard of this PHP alternative.
http://www.php.net/manual/en/features.file-upload.phpThe MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted. This is an advisory to the browser, PHP also checks it. Fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. The PHP settings for maximum-size, however, cannot be fooled. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed.