[Javascript] Finding file size using java script

Richard Lewis richardlewis at fastmail.co.uk
Mon Mar 7 08:35:16 CST 2005


On Mon, 7 Mar 2005 18:50:03 +0530 , "Abhishek_Ghosh"
<Abhishek_Ghosh at satyam.com> said:
> Hi,
>
>            I am working on jsp’s and have an issue in uploading files. I am allowed to upload at the max of 20MB on the server, but the issue is often user ignores the message >and tries to upload more than 20MB. The request goes to the server and the server then throws an error message. This process however takes some time to get executed. I >would like to have a client side validation done for the size of the file. To do this I cannot use ActiveX objects as the business requirements strictly restricts use of ActiveX >Objects. The other option is to use the following code :

>       var fileObj = document.fileSizeChecker.fileUpload.value;
>       var myFile = new File(document.fileSizeChecker.fileUpload.value);
>      myFile.open("r");
>      alert('myFile length: ' + myFile.getLength());

 >           Using the above code produces the following javascript error “’File’ is undefined”. Please suggest how to bail out of this error or an alternate method of achieving the >purpose.

File is undefined because there is not a File class in standard
JavaScript (yet, see http://www.mozilla.org/js/js-file-object.html).
Also, I don't think that 'document' has such a property as
fileSizeChecker either.

And there isn't really a way that your browser can be given permission
to fiddle around with local files. What you can do is use the 'accept'
attribute of the 'input type="file"' tag to restrict the MIME types of
files which can be uploaded but thats about it.

Cheers,
Richard





More information about the Javascript mailing list