jQuery Plugin to validate File Types and Extensions in ASP.Net AsyncFileUpload Control
 
Author:
Filed Under: ASP.Net  |  JQuery  |  AJAX Control Toolkit  |  jQuery Plugins
Published Date: Mar 08, 2011
Views: 3790
 

Abstract: Here Mudassar Ahmed Khan has explained how to use jQuery Plugin to validate file extensions and types in AJAX Control Toolkit AsyncFileUpload control

Comments:  1

 

I have already written an article on how to validate the file types and file extensions in AJAX Control Toolkit AsyncFileUpload Control. You can refer it using the link provided below
Now using the same script I have created a jQuery plugin to make it easy to use. So here’s what you need to do to use it
HTML Markup
Below is the HTML Markup and it has an AJAX Control Toolkit AsyncFileUpload Control and a Label Control to display the error message. That’s all we need to make our plugin work.
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager>
<cc1:AsyncFileUpload runat="server" ID="AsyncFileUpload2" Width="400px" UploaderStyle="Modern" CompleteBackColor="White" UploadingBackColor="#CCFFFF" ThrobberID="imgLoader" OnUploadedComplete="FileUploadComplete" />
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/loader.gif" />
<br />
<asp:Label ID="Label1" runat="server" style = "color:red" Text = "Upload a valid file with extension doc, docx." />
</form>
 
Plugin Usage
Now it’s time to see the jQuery plugin in action.
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/AsyncFileUploadValidationPlugin.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("#<%=AsyncFileUpload1.ClientID %>").Validate(
    {
        MessageControlId: "<%=lblMesg.ClientID%>",
        ValidFileExtensions: ["doc", "docx"]
    });
});
</script>
 
The plugin has two options that you need to specify
1. MessageControlId - Id of the control which will display the message when the validation fails.
2. ValidFileExtensions – Extensions of the File types which the user is allowed to upload.
That’s all you need to do to make it work.
Note:- If you do not specify the valid file extensions then by default all file types will be allowed
 
Demonstration


Downloads
You can download the complete source code and the plugin using the download link provided below.
ValidationInAsyncFileUploadusingjQuery.zip

The plugin is also submitted on the jQuery site. You can refer it using the following link








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