Dot Net Lover

Thursday, November 30, 2006

Checking File Type Upload in Javascript !!!

hi!

Many times we need to check the file types a user is uploading.

Below is the code Written in Javascript where you can check the type of file user uploading and Alerting the user so was to upload only the allowed file type.

function check()
{

if(this.document.Form1.fImage.value !="" && this.document.Form1.fImage.value.toUpperCase().indexOf(".JPG") == - 1 && this.document.Form1.fImage.value.toUpperCase().indexOf(".GIF") == -1)
{
alert("Image should be in gif or jpg format");
this.document.Form1.fImage.focus();
return false;
}
return true;
}


Above Code will alert the user if the file type is not in jpg or gif format.....

1 Comments:

  • But still nothing prevents the user from renaming a harmful virus file to a .JPG.

    The best solution would be a serverside scan and do a MimeType Check. I have also a little article on Image Validation here.

    By Anonymous Anonymous, at 5:47 AM  

Post a Comment

<< Home