Client side file size and type validation
Hello there,
I have a simple web form with a file upload input type like this
<input type="file" name="upload" />
Here is jQuery code where I'm checking for file size. For some reason, the control is not getting passed into this function. This should display alert if someone tries to upload file larger than 1 MB but I don't get alerts and can upload files larger than 1 MB. Can someone point out what mistake I'm making? I'm not getting any errors.
<script type="text/javascript">
$(function () {
$('#upload').click(function () {
//check whether browser fully supports all File API
alert("in");
if (window.File && window.FileReader && window.FileList && window.Blob) {
//get the file size and file type from file input field
var fsize = $('#upload')[0].files[0].size;
alert("in");
if (fsize > 1048576) //do something if file size more than 1 mb (1048576)
{
alert(fsize + " bites\nToo big!");
} else {
alert(fsize + " bites\nYou are good to go!");
}
} else {
alert("Please upgrade your browser, because your current browser lacks some new features we need!");
}
});
});
</script>