Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check to see if image exists

Status
Not open for further replies.

dinger2121

Programmer
Sep 11, 2007
439
US
Hello,
I have a web application that I am building. I have an images folder in my application. In my codebehind, I have to check to see if an image exists, if not, I apply a default image. I tried using

Request.Url.Host + "/Images/image.jpg";

but that doesn't seem to work. that is returning - "localhost/Images/image.jpg", which I thought would work, but the check in my if statement isn't catching it. Any idea what I should be using?

Thanks

 
Request.PhysicalApplicationPath returns the physical location. Is that the best method to use?
 
I think you'll need to serve the image through an HTTP handler for this, passing the name via a query string. So your markup would look something like:

<img src="../../Folder/ImageHandler.ashx?imgName=image" alt="Handled Image"/>

This will allow you to return the default image if the file is not found.

An example can be found here:
Hope this helps,

Alex

[small]----signature below----[/small]
The author of the monograph, a native of Schenectady, New York, was said by some to have had the highest I.Q. of all the war criminals who were made to face a death by hanging. So it goes.

My Crummy Web Page
 
On the server side, to work with a local resource, you need to get its physical location (which drive/folder). Read on Server.MapPath()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top