Check File type at client side – jQuery


Whenever we use fileupload control on web pages , the common requirement is to validate its file type and size.

we usually find it difficult to validate file size on client side rather than at server side. As of now there is no alternative script found that can achieve this. There are certain alternatives to achieve above validation on client side but that are not browser compatible & as security level changes in browser it stop functioning, so we still not at that level of trust that we can check file size at client side using jQuery/javascript.

Although it is possible to validate filetype using jquery/javascript and i m looking to do some coding for you to achieve the same. i hope this will help some developers to validate file type at client side rather than checking it on server.

Here is the javascript code that achieve above functionality :


 <script src="Js/jquery-1.4.2.min.js" type="text/javascript"></script>

    <script type="text/javascript" language="javascript">

        function FileTypeValidate() {

            //get filepath from fileupload control on the page
            var fileUpload = $('#<%=FileUpload1.ClientID %>').val();

            //extracting part of the filename from dot
            var extension = fileUpload.substring(fileUpload.lastIndexOf('.'));

            //valid file type - static
            var ValidFileType = ".jpg , .png , .bmp";
            //or fetch it from config file for flexibility ,
	    //we can save valid file type list in web.config also & fetch it during validation process
            var ValidFileTypeConfig = '<%=ConfigurationManager.AppSettings["ValidFileType"].ToString() %>';

            //check whether user has selected file or not
            if (fileUpload.length > 0) {

                //check file is of valid type or not
                if (ValidFileType.toLowerCase().indexOf(extension) < 0) {
                    alert("please select valid file type...");
                }
                else {
                    alert("file type is valid...");
                    return true;
                }
            }
            else {
                alert("please select file for upload...");
            }
            return false;
        }
    </script>

Above code explain itself a lot so no need to discuss further on it.

Here is the html markup that shows controls on the page :


    <form id="form1" runat="server">
     <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClientClick="return FileTypeValidate();" />
     </div>
    </form>

This is very simple but very common scenario for the web applications & very effective if managed properly…

Thats it, hope this will help !!!

Jay Ganesh

Advertisements

One thought on “Check File type at client side – jQuery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s