We help IT Professionals succeed at work.

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

SystemSys
SystemSys asked
on
Medium Priority
993 Views
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

Comment
Watch Question

Top Expert 2009
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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
 
        Try
 
            If Me.img.PostedFile.FileName = "" Then
 
                Me.pnl_error.Visible = True
                Me.lbl_error.Text = "No photo was attached."
                Exit Sub
 
            Else
 
                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

Faizan SarwarSoftware / Database Developer
CERTIFIED EXPERT

Commented:
string ext=System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        if (ext.Equals(".jpg") ||ext.Equals(".gif")||ext.Equals(".png"))
{
//do smth
}else
        {
            return;
        }

Commented:
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

Commented:
thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.