在服务器上找不到图像时渲染默认图像

时间:2014-03-20 06:25:58

标签: javascript image iis-7

我有一个网页,可以从服务器呈现图像。如果找不到映像,则当前IIS服务器返回404错误,因此不会呈现图像。

我要求在服务器上找不到图像时放置默认图像。

我可以在IIS服务器上进行此设置,以便为每个无效的图像请求返回默认图像吗?

2 个答案:

答案 0 :(得分:1)

您可以对未找到的图片使用javascript onerror()事件:

function imgError(image) {
        image.onerror = "";
        image.src = "somePlaceholderImage.gif";
        return true;
 }

然后你的图像会是这样的:

<img alt="some ALT text" src="http://someurlhere" onerror="imgError(this);">

答案 1 :(得分:0)

   <rewrite>
            <rules>
                <rule name="RewriteNoneExist">
                    <match url="\.(gif|jpe?g|png|bmp)"  ignoreCase="true" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="gfx/default.png" />
                </rule>
            </rules>
        </rewrite>