Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net check if imageURL exists

I am trying to get a user's thumbnail from another intranet site but some of them do not follow the pre-defined format meaning I would want to load up a default thumbnail instead.

Whats the best way to check if an image URL is valid?

like image 687
sd_dracula Avatar asked May 30 '12 08:05

sd_dracula


1 Answers

Depending on how you are getting your images a variation of this might work

<html>
    <body>
        <img src="<dynamic handler url>" alt="My Username" onError="this.src='defaultProfile.jpg';" />
    </body>
</html>

This is how you would do it in ASP.NET.

Designer -

<asp:Image ImageUrl="NonexistentImage.Jpg" ID="profileImage" Height="50" Width="50" runat=server />

Code Behind (c#)

profileImage.Attributes["onerror"] = "this.src='http://www.cs.uofs.edu/~olivetoj2/blah.jpg';";

This works perfectly for me.

like image 161
Mike Miller Avatar answered Oct 18 '22 09:10

Mike Miller