File type and extension validation in ASP.Net AsyncFileUpload Control using JavaScript
 
Author:
Filed Under: ASP.Net  |  JavaScript  |  AJAX Control Toolkit
Published Date: Mar 07, 2011
Views: 6012
 

Abstract: Here Mudassar Ahmed Khan has explained how to validate File type using its extension in ASP.Net AJAX Control Toolkit AsyncFileUpload control client side using JavaScript

Comments:  2

 

Note: For jQuery lovers I have created a plugin to do the job, you can refer the following article for more details
In this article I am explaining how to validate the ASP.Net AJAX Control Toolkit AsyncFileUpload control. By default this control does not have any validation property and also you cannot use ASP.Net Validators with this control.
There are articles on internet who validate after the file is uploaded on the OnClientUploadComplete event, which to me does not make sense as what’s the use of validating once file is already saved on server. If it is a virus or some malicious program it will do its job.
 
Hence I came up with the following solution and it is actually a hack to make the ASP.Net AJAX Control Toolkit AsyncFileUpload control actually validate the file before uploading. Don’t worry you will not need to do anything you just need to copy the script and use it with minimal changes
 
HTML Markup
 
Below is the HTML markup which has nothing but an AsyncFileUpload control, a Throbber and a label to display the validation messages
 
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<cc1:AsyncFileUpload runat="server" ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern" CompleteBackColor="White" UploadingBackColor="#CCFFFF" ThrobberID="imgLoader" OnUploadedComplete="FileUploadComplete" />
<asp:Image ID="imgLoader" runat="server" ImageUrl="~/images/loader.gif" />
<br />
<asp:Label ID="lblMesg" runat="server" style = "color:red" Text = "Upload a valid file with extension doc, docx." />
</form>
 
 
Client Side File Extension Validation script
 
Below is the script that you need to paste in the head section of the page or master page or on the Content Page within SCRIPT tags
 
<script type="text/javascript">
    var errorMessage;
    window.onload = function () {
        errorMessage = $get("<%=lblMesg.ClientID %>").innerHTML;
        $get("<%=lblMesg.ClientID %>").innerHTML = "";
        AjaxControlToolkit.AsyncFileUpload.prototype._onStart = function () {
            var valid = this.raiseUploadStarted(new AjaxControlToolkit.AsyncFileUploadEventArgs(this._inputFile.value, null, null, null));
            if (typeof valid == 'undefined') {
                valid = true;
            }
            if (valid) {
                valid = Validate(this._inputFile.value);
                if (!valid) {
                    this._innerTB.value = "";
                    this._innerTB.style.backgroundColor = this._completeBackColor;
                }
            }
            return valid;
        }
    }
    var validFilesTypes = ["doc", "docx"];
    function Validate(path) {
        $get("<%=lblMesg.ClientID%>").innerHTML = "";           
        var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
        var isValidFile = false;
        for (var i = 0; i < validFilesTypes.length; i++) {
            if (ext == validFilesTypes[i]) {
                isValidFile = true;
                break;
            }
        }
        if (!isValidFile) {
            $get("<%=lblMesg.ClientID %>").innerHTML = errorMessage;               
        }
        return isValidFile;
    }
</script>
 
The extensions you want to allow you can specify in the validFilesTypes array separated by comma.
That’s all you need to do to enable validation for ASP.Net AJAX Control Toolkit AsyncFileUpload control.
 
Demonstration

 
Downloads
 
You can download the complete source code using the download links provided below

ValidationInAsyncFileUpload.zip








Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit