Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}
Posted
Updated 25-May-19 23:36pm

1 solution

The image url is going to be the location of the image on the server's drive which the client obviously can't access. The image url has to be an http path to the image on the server. Try setting ImageUrl to virtualPath instead, you might need something like

ImageButton1.ImageUrl = VirtualPathUtility.ToAbsolute(virtualPath);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900