Click here to Skip to main content
15,212,479 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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);
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100