Image URL is correct but image not showing
Asked Answered
T

2

7

I have a website on GoDaddy. All permissions are set correctly and the image DOES exist. However when the page loads the image for the item selected does not show. Here is my code

        imagepath = "~/spaimages/" + currentSpaModel.Name.ToString() + ".png";
        if (File.Exists(Server.MapPath(imagepath)))
        { this.spaimage.ImageUrl = Server.MapPath(imagepath); }

spaimage is an ASP control and thr URL that the image is set to is D:\hosting\xxxxxxx\calspas\spaimages\modelname.png

What am I doing wrong.

Taw answered 12/1, 2011 at 3:45 Comment(0)
F
15

The file path D:\hosting\xxxxxxx\calspas\spaimages\modelname.png is the folder where the image resides on the web server. You are sending this as the <img> tag's src attribute, which tells the browser, "Go get the image at D:\hosting\xxxxxxx\calspas\spaimages\modelname.png." The browser cannot go off to the D drive of the web server, so it looks on its own D drive for that folder and image.

What you mean to do is to have the <img> tag's src attribute be a path to a folder on the website. You're just about there - just drop the Server.MapPath part when assigning the image path to the ImageUrl property. That is, instead of:

this.spaimage.ImageUrl = Server.MapPath(imagepath);

Do:

this.spaimage.ImageUrl = imagepath;

See if that works.

Thanks

Fontainebleau answered 12/1, 2011 at 3:54 Comment(0)
K
2

Often, if an image "does not show" (I assume a red-x-equivalent is being displayed to show "broken image"), I right-click the broken image, copy the URL and open the URL in a separate browser window.

This way, when the image is being generated by some script, I see any error text that the script might have shown. If not, the real image would be displayed.

In addition, add an else block to the

if (File.Exists(Server.MapPath(imagepath)))

like

else 
{ 
    Response.Write(string.Format(
        "File does not exist at '{0}'.", 
        Server.MapPath(imagepath))); 
}

For debugging purposes.

Kershaw answered 12/1, 2011 at 3:48 Comment(1)
Actually the asp image control shows a tiny box without a red x. and when I right click to view image properties nothing is there but when I go to view source the URL is present.Taw

© 2022 - 2024 — McMap. All rights reserved.