Page 1 of 1
determine file size from JS
Posted: Tue Mar 08, 2005 5:00 pm
by Burrito
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--
Posted: Tue Mar 08, 2005 5:24 pm
by PrObLeM
im pretty sure its not possible unless you have the user input the filename manually but thats not a good idea
Posted: Tue Mar 08, 2005 5:44 pm
by feyd
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..
Posted: Tue Mar 08, 2005 6:14 pm
by Burrito
any ideas where I might such a post?
Posted: Tue Mar 08, 2005 6:34 pm
by Burrito
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;
}
Posted: Tue Mar 08, 2005 8:50 pm
by wyred
Just in case you haven't heard of this PHP alternative.
The 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.
http://www.php.net/manual/en/features.file-upload.php