显示默认的图像,如果指定的图像文件没有找到最好的方法? [英] Best way to display default image if specified image file is not found?
问题描述
我有一般的电子商务应用程序,我ITEM_IMAGE_NAME存储在数据库中,有时经理拼错图像名称。
要避免丢失图像(在IE红色的X),每次我展示的产品清单的时候,我检查服务器是否有相关的产品形象,并且如果该文件不存在 - 我将其替换为默认的图像。
至于我可以告诉这并不影响性能,但我想知道如果有任何的替代品来修复一个失踪形象的问题。
我使用ASP.NET + C#(.NET 3.5)
有些code:
的foreach(在项目项目项目)
{
字符串路径= Path.Combine(〜/图片/,item.categoryImage);
item.categoryImage = File.Exists(使用Server.Mappath(路径))? item.categoryImage:GetImageDependingOnItemType();
}
您可以考虑使用javascript的东西。
< IMG SRC =image.jpg文件的onerror =this.onerror = NULL; this.src ='default.jpg'>
编辑:或者程序404.aspx返回一个默认的图像,如果请求一个不存在的图像
I've got your average e-Commerce app, I store ITEM_IMAGE_NAME in the database, and sometimes managers MISSPELL the image name.
To avoid "missing images" (red X in IE), every time I display the list of products, I check the server for the image related to the product, and if that file doesn't exist - I replace it with default image.
As far as i can tell this doesn't affect performance, but I was wondering if there are any alternatives to fix a "missing image" problem.
I'm using ASP.NET + C# (.NET 3.5)
Some code:
foreach (Item item in Items)
{
string path = Path.Combine("~/images/", item.categoryImage);
item.categoryImage = File.Exists(Server.MapPath(path)) ? item.categoryImage : GetImageDependingOnItemType();
}
You might consider something with javascript
<img src="image.jpg" onerror="this.onerror=null;this.src='default.jpg'">
Edit: Or program a 404.aspx returning a default image, if a nonexisting image was requested.
这篇关于显示默认的图像,如果指定的图像文件没有找到最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!