asp.net检查是否存在IMAGEURL(asp.net check if imageURL ex

2019-07-29 15:46发布

我想从另一个内部网站用户的缩略图,但他们中的一些不遵循预先定义的格式,这意味着我会想,而不是加载了一个默认的缩略图。

请告诉我要检查的最佳方式,如果图片的网址是有效的?

Answer 1:

根据你如何让你的图片此变化可能会奏效

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

这是你会怎么做,在ASP.NET。

设计师 -

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

后面的代码(C#)

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

这完全适用于我。



Answer 2:

WebRequest webRequest = WebRequest.Create(url);  
WebResponse webResponse;
try 
{
  webResponse = webRequest.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
  return 0;
} 
return 1;


Answer 3:

你可以在jQuery中很容易达致这。

$("#myImage")
    .load(function() { alert("it loaded ok") })
    .error(function() {  $(this).attr("src", alternateImage)  });


Answer 4:

从代码隐藏检查

File.Exists(Server.MapPath("file path"))

如果返回true,则指定的值,否则您指定默认的缩略图。



文章来源: asp.net check if imageURL exists