views:

472

answers:

6

I have upload file functionality on one of the page. I check for the extension of the file using JavaScript. Now i want to restrict the user from uploading file greater than 1 MB. Is there any way i can check the file upload size using JavaScript.

My code currently look like this:

<script language="JavaScript">
function validate() {
   var filename = document.getElementById("txtChooseFile").value;
   var ext = getExt(filename);
   if(ext == "txt" || ext == "csv")
      return true;
   alert("Please upload Text files only.");
   return false;
}

function getExt(filename) {
   var dot_pos = filename.lastIndexOf(".");
   if(dot_pos == -1)
      return "";
   return filename.substr(dot_pos+1).toLowerCase();
}
</script>
+4  A: 

Other that aquiring the filename there is no way for you to find out any other details about the file in javascript including its size.

Instead you should configure server-side script to block an oversized upload.

AnthonyWJones
Also, even if you could you'll want a server-side check anyway, since there's nothing to stop malicious users writing their own form that posts to the same place.
Dominic Rodger
A: 

It might be possible using a lot of browser-specific code. Take a look at the source of TiddlyWiki, which manages to save itself on the user's hard drive by hooking into Windows Scripting Host (IE), XPCOM (Mozilla), etc.

Anthony Mills
A: 

I don't think there is any way of doing that with plain JS from a web page.
With a browser extension maybe, but from a page javascript cannot access the filesystem for security reasons.

Flash and Java should have similar restrictions, but maybe they are a bit less strict.

Victor
A: 
moxn
I just tried this out (Firefox 3) and the file is still submitted, even if you set the maxlength to something really low. The W3C specs only mention maxlength as affecting the number of characters in a field, nothing to do with file sizes.
nickf
Hm, okay. That's true. I checked again and apparently `maxlength` was supposed to do this for file inputs in HTML 3.2 :) But from 4.01 on, this has been omitted. (for a german source see here: http://de.selfhtml.org/html/formulare/datei_upload.htm). I will edit my post. Thanks.
moxn
A: 

not possible. would be a major security concern to allow client side scripts to run that can read file info from and end users hard drive.

Aliixx
This is supported in Firefox 3.6. See HTML 5's <http://www.w3.org/TR/FileAPI/>. I've used that module, and it works great for uploading via AJAX without needing any sort of Flash objects or iframe trickery.
konforce
Sorry, link should obviously be http://www.w3.org/TR/FileAPI/.
konforce
+1  A: 

See http://www.w3.org/TR/FileAPI/. It is supported by Firefox 3.6; I don't know about any other browsers.

Within the onchange event of a <input id="fileInput" type="file" /> simply:

var fi = document.getElementById('fileInput');
alert(fi.files[0].size); // maybe fileSize, I forget

You can also return the contents of the file as a string, and so forth. But again, this may only work with Firefox 3.6.

konforce