upload only gif/jpg/bmp images

Posted on 2004-11-21
Last Modified: 2012-08-13
how to validate the image type in

i want to restrict to the user to upload only gif/jpg/bmp images

what is the best way to restrict?

Question by:service2001

    Author Comment

    i need the code to pupup alert message that, you are allowed to upload gif/jpg images.

    is there any code can do that?
    LVL 6

    Expert Comment

    You can display a javascript alert message when the user clicks on a button to upload the image.

    <input type="submit" name="Button1" value="Button" id="Button1"  onclick="javascript:alert('you are allowed to upload gif/jpg images.');" />

    Now in your button click handler on the server, simply validate the extension of the file using the fileinfo classe's "Extension" property

    Author Comment

    can i have a complete code please?
    LVL 6

    Expert Comment

    You can also use System.IO.Path Class in .NET

    string fileName = @"C:\mydir.old\myfile.ext";
    string extension;

    extension = Path.GetExtension(fileName);

    Author Comment

    but how do i check on client side?


    Author Comment

    if you have done similar, would you paste the code here for my reference? thank you!
    LVL 7

    Accepted Solution

    <TD align="right">
    <asp:label id="lblUploadLogo" runat="server"></asp:label>
    <TD colSpan="2">
    <INPUT id="fileupload1" style="WIDTH: 299px; HEIGHT: 22px" type="file" size="30" name="fileupload1"

    <asp:image id="imgLogoFile" runat="server" Width="30px" Height="24px"></asp:image>
    <asp:RegularExpressionValidator id="Regularexpressionvalidator1" runat="server" ErrorMessage="Select valid file *.gif or *.jpg or bmp file!"

    ControlToValidate="fileupload1" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.gif|.bmp|.jpg)$"></asp:RegularExpressionValidator></TD>

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now