Monday, January 10, 2011

How to check the Extension of file uploaded by file upload control using Javascript

Javascript Code:
function ValidateFileUpload_flv(fuc) {
          var fuData = document.getElementById(fuc);
          var FileUploadPath = fuData.value;

          if (FileUploadPath == '') {
              return false;
          }
          else {
              var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
              if (Extension == "flv") {

                  return true;
              }
              else {
                  fuData.value = "";
                  alert("You can upload only FLV file");
                  return false;

              }
          }
      }



Aspx code:
Add a file upload control "FileUpload1" to your page and in the page load event of the page write the following code to bind the function for checking the extension with the change event of this fileupload control.


FileUpload1.Attributes.Add("OnChange", "return ValidateFileUpload_flv(this.id);");





No comments:

Post a Comment