Upload allow only image files in the formats .jpg, .gif and .png

Posted on 2009-02-18
Last Modified: 2012-05-06
greetings to you
sir, experts

how i can upload only image files only
files in the formats .jpg, .gif and .png using FILE_UPLOAD CONTROL
PLEASE GIVE ME CLEAR Example because i'm tire from searching in web

please reply as soon as possible

my best regards

Question by:SystemSys
    LVL 27

    Accepted Solution

    LVL 8

    Expert Comment

    Here is a piece of sample code from a previous project. Pretty much you want to evaluate the file extension.
        Protected Sub cmd_upload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmd_upload.Click
                If Me.img.PostedFile.FileName = "" Then
                    Me.pnl_error.Visible = True
                    Me.lbl_error.Text = "No photo was attached."
                    Exit Sub
                    Select Case Right(Me.img.PostedFile.FileName, 3).ToLower()
                        Case "jpg", "gif", "png"
                        'add code to save file
                        Case Else
                            Me.pnl_error.Visible = True
                            Me.lbl_error.Text = "The file format you attached is not supported."
                            Exit Sub
                    End Select
                End If
            Catch ex As Exception
                Me.pnl_error.Visible = True
                Me.lbl_error.Text = ex.Message
            End Try
        End Sub

    Open in new window

    LVL 13

    Expert Comment

    string ext=System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
            if (ext.Equals(".jpg") ||ext.Equals(".gif")||ext.Equals(".png"))
    //do smth
    LVL 10

    Expert Comment

    This is what i suggest.
    List<string> oAvailExtensions = new List<string>(){".jpg",".gif",".png"};
    if (FileUpload1.HasFile){
        string sEx = Path.GetExtension(FileUpload1.PostedFile.FileName);
        if (oAvailExtensions.Contains(sEx))

    Open in new window


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now