In my ASP.NET WebForms project, I have a folder called 'Uploads' that contains a few .jpg photo image files. If I right-click it and select "Open Folder in File Explorer", it shows my five image files listed, and each one has a '.jpg' extension.
I have an ImageButton control on the form.
I select the ImageButton control and in the properties window.
I click the ellipsis button for ImageUrl,
I select the 'Uploads' folder.
Whether I select "Image Files(*gif,*jpg,*jpg,*bmp,*wmf,*.png)" or even "All Files(*.*)", in either case, it shows an empty file list, as if there are no files in the folder.
As a result, when I assign the full file path to the ImageButton1.ImageUrl, the image does not appear but remains blank. But again even at design time the ImageButton is not seeing these image files at all. How can I fix this?
I made sure that the
Static Content option is selected in WWW Service \ Common HTTP Features.
What I have tried:
HttpPostedFile postedFile = FileUpload1.PostedFile;
string fileName = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(fileName);
int fileSize = postedFile.ContentLength;
string[] exts = { ".jpg", ".bmp", ".gif", ".png" };
if (exts.Contains(fileExtension.ToLower()))
{
var dirUploads = "~/Uploads/";
var virtualPath = dirUploads + fileName;
var filePath = System.Web.HttpContext.Current.Server.MapPath(virtualPath);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
postedFile.SaveAs(filePath.ToString());
ImageButton1.ImageUrl = filePath.ToString();
}