How can I get a file's upload size using simple Javascript?
Posted
by Pankaj Khurana
on Stack Overflow
See other posts from Stack Overflow
or by Pankaj Khurana
Published on 2009-10-22T12:23:32Z
Indexed on
2010/03/19
3:51 UTC
Read the original article
Hit count: 304
JavaScript
|upload
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>
© Stack Overflow or respective owner