【问题标题】:Embedding svg in aspx.net does not work with IIS 7.5在 aspx.net 中嵌入 svg 不适用于 IIS 7.5
【发布时间】:2019-06-09 18:33:51
【问题描述】:

我有一个网站,直到最近才通过 aspx 在另一个 aspx 中嵌入了一个 png 图像,生成的 html 代码如下所示:

   <div class="widgetcontent" style="padding: 0px; margin: 0px; padding-left: 5px;">
     <div id="ctl00_CPHMaster_UPRanges">
         <img id="ctl00_CPHMaster_IMRanges" src="range.aspx" style="height:280px;width:460px;border-width:0px;" />
     </div>
   </div>

range.aspx 只提供了一个动态创建的 png 图像。

我现在更改了 range.aspx 以便它提供一个 svg 并更改了嵌入页面,因此生成的 html 代码现在看起来像这样:

  <div class="widgetcontent" style="padding: 0px; margin: 0px; padding-left: 5px;">
    <div id="ctl00_CPHMaster_UPRanges">
        <img id="ctl00_CPHMaster_IMRanges" type="image/svg+xml" src="range.aspx" style="height:280px;width:460px;border-width:0px;" /> 
    </div>
  </div>

但是,这并不正确。这些是事实:

a) 它可以在我的 Windows 开发人员机器上使用由 VisualStudio 2012 创建的 ASP.NET 开发服务器正常工作。

b) 它可以在运行 IIS 8.5 的测试机器上运行。

c) 它不适用于运行 IIS 7.5 的生产机器。

d) “它不起作用”的意思是:应该显示嵌入 svg 的页面只是显示了一个损坏的图像标志。

e) 但是当我右键单击损坏的图像标志并选择“查看图像”时,会显示 svg!

f) 当我右键单击唯一的 svg 并选择“查看页面源代码”时,它显示纯 svg 文本,周围没有 html。

g) 网上有些地方写到 svg 必须在 IIS 7.5 配置中启用。我已经这样做了。它仍然不起作用。

h) 这不是浏览器的问题,我用 Chrome、IE、Firefox 等检查过。

任何帮助表示赞赏。

更新1:因为Brando Zhang询问range.aspx代码,原则上是这样的:

<%@ Import Namespace="WVA.Client.Web" %>

<%@ Page Language="c#" AutoEventWireup="true" Culture="en-Us" UICulture="auto" %>

<%@ Import Namespace="WVA.ServiceAccessLayer.WebService" %>
<%@ Import Namespace="WVA.SharedBusinessLogic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
<head>

    <script runat="server" language="C#">           

        protected void Page_Load(object sender, EventArgs e)
        {

            Response.Clear();           

            byte[] imageByteData = funcGetImage(bildformat);
            if (format == Format.Svg)
            {
                Response.ContentType = "image/svg+xml";
                Response.Flush();
                string imageTextUtf8 = Encoding.UTF8.GetString(imageByteData, 0, imageByteData.Length);
                Response.Write(imageTextUtf8);
            }
            else if (format == Format.Png)
            {
                Response.ContentType = "image/png";
                Response.Flush();
                Response.BinaryWrite(imageByteData);
            }

            Response.End();
        }

    </script>

</head>
</html>

更新 2:由于 Hackerman 的提示,我尝试将其添加到 web.config:

    <staticContent>
        <remove fileExtension=".svg" />
        <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
    </staticContent>

这并没有让嵌入图像变得更好。但它有一个有趣的效果,后来在 Chrome 和 Opera 中,“在单独的选项卡中查看图像”不再显示 svg 图像而是 svg 文本。 Firefox 仍然显示图像。

更新 3: 在 Lex Li 的提示下,我们检查了 Chrome 的开发者工具。在那里找不到任何错误。但有趣的是: 在一切正常的地方,响应标头包含该行 内容类型:image/svg+xml。在 IIS 7.5 中,事情不正常,响应标头中没有 Content-Type:

【问题讨论】:

  • 可能相关,查看本帖stackoverflow.com/questions/12328651/…的asnwer
  • 能分享一下range.aspx的详细代码吗?此页面是否生成 png 和 SVG?在页面加载事件中直接使用 response.write 吗?
  • “d) “它不起作用”的意思是:应该显示嵌入 svg 的页面只是显示了一个损坏的图像标志。”浏览器开发者工具应该告诉你的不仅仅是损坏的图像标志。先检查一下。
  • 在更新 2 中,您为以 .svg 结尾的文件设置 mime 类型,您的文件不以 .svg 结尾,其扩展名为 .aspx
  • @Robert Longson:谢谢。我无法将 aspx 的一般 mime 类型设置为 image/svg+xml。在我发现它没有帮助之后,我已经从 web.config 中删除了该代码。

标签: asp.net iis svg


【解决方案1】:

我们已经找到了问题的解决方案。 最初我们有这个代码来创建图像:

            Response.ContentType = "image/svg+xml";
            Response.Flush();
            string imageTextUtf8 = Encoding.UTF8.GetString(imageByteData, 0, imageByteData.Length);
            Response.Write(imageTextUtf8);

这不适用于 IIS 7.5,Response.Flush() 似乎删除了 IIS 7.5 中的 Content-Type。 (但不在 IIS 10 或 ASP.NET Development Server 11 中。)

使用以下代码,不会删除 Content-Type 并且一切正常。

            Response.ContentType = "image/svg+xml";
            string imageTextUtf8 = Encoding.UTF8.GetString(imageByteData, 0, imageByteData.Length);
            Response.Write(imageTextUtf8);
            Response.Flush();

【讨论】:

    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    相关资源
    最近更新 更多