How can you program a FileUpload control in ASP.Net 3.5 to browse for Excel files only

Posted on 2011-04-28
Last Modified: 2012-06-27
Hi Experts,
I have an ASP.Net 3.5 application with VB.Net codebehind.  I use a FileUpload control in my application to allow the user to browse for a given Excel file.  How can i program the FileUpload control so that it only searches for Excel files (files with the .xls extension only)

Thank you in advance,
Question by:mainrotor
    LVL 17

    Expert Comment

    by:Carlos Villegas
    Hi, Is not exactly what your are asking for but I think that fit into your scenario:
    LVL 17

    Expert Comment

    by:Carlos Villegas
    By the way, there is not way (in my knowledge) to apply a filter to the web browser file dialogue, but the technique in that link is good to workaround that.
    LVL 3

    Expert Comment

    Try using the code given below. This is not exactly what you want but you help you restrict the files to be selected are only excel

    if (filename.indexOf(".xls") == -1) {
      alert(filename + " in not an excel file");
    } else {
      // process the file...
    LVL 9

    Accepted Solution

    We can not apply filter to web browser file dialogue. But you can restrict file upload control to pick only excel files with following code.

    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUploadControl" runat="server" />
        <asp:RegularExpressionValidator ID="regexpName" runat="server"    
                                        ErrorMessage="This is not valid excel."
                                        ValidationExpression="^(.*?).(xls|XLS|xlsx|XLSX)$" />

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now