asp:FileUpload file validation. [Resolved]

Posted by Allemahesh under ASP.NET on 3/19/2014 | Points: 10 | Views : 877 | Status : [Member] [MVP] | Replies : 1
I am using asp:FileUpload in my web site. I am checking the file size and file type on button click event. It is working fine.
My question is how to check file size and file type when user select the file form asp:FileUpload.
I mean to say that I want to show the alert window on selecting the file.




Responses

Posted by: Allemahesh on: 3/19/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down

Resolved
onchange="return GetFileName(this);" is worked for me. Here what I did,

<script type="text/javascript">

function GetFileName(myfile) {

var FileName = myfile.value;
FileName = FileName.substring(FileName.lastIndexOf('\\') + 1);
var FileExt = FileName.substring(FileName.lastIndexOf('.') + 1);
if ((FileExt == 'gif') || (FileExt == 'GIF') || (FileExt == 'jpeg') || (FileExt == 'JPEG') || (FileExt == 'jpg') || (FileExt == 'JPG') || (FileExt == 'png') || (FileExt == 'PNG') || (FileExt == 'bmp') || (FileExt == 'BMP')) {

}
else {
myfile.outerHTML = myfile.outerHTML;
alert('Please upload an image file');
}

//This will give you the file size in bytes.
var size = myfile.files[0].size;
var ImageFileSizeInKB = 2048;
var _fileSizeInByte = parseInt(ImageFileSizeInKB) * 1024;

if (size > _fileSizeInByte) {
myfile.outerHTML = myfile.outerHTML;
alert("File size should not be greater than " + (ImageFileSizeInKB / 1024) + " MB.");
}

return true;
}
</script>




<asp:FileUpload ID="FileUpload1" runat="server" size="40" onchange="return GetFileName(this);" />


Happy Coding.

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response