Check for valid file in FileUpload Control using JavaScript


Here is the code by which you can check for valid file on client side.

Here Is the javaScript Code for validate File on Client Side

here a array is used to store valid file extensions which u want to upload on server
for multiple extensions just add Extensions in validExtensions array ilke this


validExtensions[0] = 'pdf';
validExtensions[1] = 'doc';




<script language="javascript" type="text/javascript">
function checkFileExtensionLogo(elem,obj)
{
var filePath = elem.value;

if(filePath.indexOf('.') == -1)
return false;

var validExtensions = new Array();
var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();

validExtensions[0] = 'pdf';
validExtensions[1] = 'doc';


for(var i = 0; i < validExtensions.length; i++) {
if(ext == validExtensions[i])

return true;
}

alert('Invalid file, only pdf and doc files are allowed.');
var who=document.getElementsByName(obj)[0];

who.value="";var who2= who.cloneNode(false);
who2.onchange= who.onchange;

who.parentNode.replaceChild(who2,who);

return false;
}

</script>




Add attribute on fileupload control for using this javascript

Here is the Code



FileUpload1.Attributes.Add("onchange", "return checkFileExtensionLogo(this,'" + FileUpload1.ClientID + "');");


Related Articles

Upload and Compressed file

By using the code you can upload file and then compressed it. It Retrieve file information and upload to server.

More articles: File upload dialog File Upload control Validation FileUpload Valid file FileUpload Javascript validation

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: