1

I have got a website which until recently embedded a png image via aspx in another aspx, the generated html code looks like this:

   <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 delivered just an png image that was created on the fly.

I have changed range.aspx now so that it delivers a svg and changed the embedding page so that the resulting html code now looks like this:

  <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>

This does not work right, though. These are the facts:

a) It does work right on my windows developers machine with the ASP.NET development server which is created by VisualStudio 2012.

b) It does work on a test machine which runs IIS 8.5.

c) It does not work on my production machine which runs IIS 7.5.

d) "It does not work" means: The page that should show the embedded svg just shows a broken image sign instead.

e) But when I right-click the broken image sign and select "View image", the svg is shown!

f) When I right click the sole svg and select "view page source", it shows pure svg text, no html around it.

g) Some places in the net write that svg must be enabled in the IIS 7.5 configuration. I did this already. It does still not work.

h) This is not a browser problem, I have checked with Chrome, IE, Firefox and others.

Any help appreciated.

Update 1: Because Brando Zhang asked about the range.aspx code, in principle it looks like this:

<%@ 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>

Update 2: Because of Hackerman's hint, I tried to add this to the web.config:

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

This did not make things better for the embedded image. But it had the interesting effect, that afterwards in Chrome and Opera, the "View image in a separate tab" did not any longer show the svg image but the the svg text. Firefox still showed the image.

Update 3: After Lex Li's hint, we've checked with Chrome's developer tool. Could not find any error there. But what is interesting: Where things are ok, the response header contains the line Content-Type: image/svg+xml. With IIS 7.5, where things are not ok, there is no Content-Type: in the response header.

AndreasW
  • 83
  • 7
  • 1
    Maybe related, check the asnwer on this thread https://stackoverflow.com/questions/12328651/svg-is-not-working-on-iis-webserver-on-localhost – Hackerman Jan 15 '19 at 17:40
  • Could you please share the details codes about range.aspx? Does this page generate the png and SVG?Directly using response.write in page load event? – Brando Zhang Jan 16 '19 at 06:41
  • 1
    “d) "It does not work" means: The page that should show the embedded svg just shows a broken image sign instead.” The browser developer tool should tell you much more than the broken image sign. Check that first. – Lex Li Jan 16 '19 at 16:24
  • In update 2 you set the mime types for files that end .svg, your file does not end with .svg, its extension is .aspx – Robert Longson Jan 22 '19 at 10:34
  • @Robert Longson: Thanks. I cannot set the general mime type for aspx to image/svg+xml. And I have removed that code from the web.config after I've seen that it didn't help. – AndreasW Jan 22 '19 at 10:56

1 Answers1

0

We've found out the solution to the problem. Originally we had this code to create the image:

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

This did not work with IIS 7.5, the Response.Flush() seems to remove the Content-Type in IIS 7.5. (But not in IIS 10 or ASP.NET Development Server 11.)

With the following code, the Content-Type is not removed and all works fine.

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