1

I have a following behind code for a fileupload witch is limited to upload only "PNG" files. How I can do to allow also "JPG" and "JPEG" files to upload?

protected void btnMainPicUPL_Click(object sender, EventArgs e)
    {
        String ext = System.IO.Path.GetExtension(fulMainPicUPL.FileName);

        if (ext == ".png")
        {
            String path = Server.MapPath("\\~/../Logged_in/AdminFotoUser/UserPics\\");
            fulMainPicUPL.SaveAs(path + txtMainPicUPL.Text + ext);
        }

        else
        {
            lblServerMSG.ForeColor = System.Drawing.Color.Red;
            lblServerMSG.Text = "<br>No hemos podido cargar tu foto!";
        }
    }
1
  • 2
    Have you tried just adding jpg and jpeg to your if statement? if (ext == ".png" || ext == ".jpg" || ext == ".jpeg") Commented Nov 21, 2013 at 20:54

1 Answer 1

3

You could store your allowed file extensions in an array, then do a Contains on it like this:

        string ext = System.IO.Path.GetExtension(fulMainPicUPL.FileName);

        string[] allowedExtenstions = new string[] { ".png", ".jpg", ".jpeg" };

        if (allowedExtenstions.Contains(ext))
        {
        string path = Server.MapPath("\\~/../Logged_in/AdminFotoUser/UserPics\\");
        fulMainPicUPL.SaveAs(path + txtMainPicUPL.Text + ext);
        }

Please note, you would be better off storing the allowed file extensions in a configurable place like the in your appSettings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.