The image URL is correct, but the image does not appear

I have a website on GoDaddy. All permissions are set correctly and the image exists. However, when the page loads, the image for the selected item is not displayed. 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 the URL URL for which the image is set: D: \ hosting \ xxxxxxx \ calspas \ spaimages \ modelname.png

What am I doing wrong.

+6
server.mappath imageurl
source share
2 answers

The path to the file D:\hosting\xxxxxxx\calspas\spaimages\modelname.png is the folder in which the image is located on the web server. You send this as the <img> tag src attribute, which tells the browser: "Go get the image in D:\hosting\xxxxxxx\calspas\spaimages\modelname.png ". The browser cannot go to disk D of the web server, so it looks at its own disk D for this folder and image.

What you want to do is the <img> tag src is the path to the folder on the website . You are just there - just release 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 this works.

thanks

+14
source share

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

Thus, when the image is generated by some script, I see the error text that the script might show. If not, the real image will be displayed.

Also add an else block to

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

as

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

For debugging purposes.

+2
source share

All Articles