Reputation: 37
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!";
}
}
Upvotes: 1
Views: 9216
Reputation: 33326
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
.
Upvotes: 3