Setting ContentType = “image/tiff” and sending an image is not working in IE

Total Post:102

Points:714
 1793  View(s)
Ratings:
Rate this:

I need to send an image (as a downloadable file) from an ASP web page. It is working correctly in every browser except for IE (all versions).

Here is the server side code:

bool export = Request.QueryString["Export"] != null;

if (export)

{

    byte[] allBytes = File.ReadAllBytes(@"C:\MyImage.tif");

    Response.ContentType = "image/tiff";

    Response.AddHeader("content-disposition", "attachment; filename=\"MyImage.tif\"");

    Response.OutputStream.Write(allBytes, 0, allBytes.Length);

    Response.OutputStream.Flush();

    Response.End();

    return;

}

And here is the JavaScript:

$('#ExportFrame').attr('src', 'Default.aspx?Export=true'); // ExportFrame is an iframe

In IE, I keep getting an error saying "Internet Explorer cannot download Default.aspx from localhost". I thought it might be an issue with loading it in an iframe element, but redirecting to the URL is not working either. The really odd thing is that going to the URL (/Default.aspx?Export=true) does not work the first time, but works every time after that. Again, this works in every browser I've tried except IE.

Any ideas?

  1. Post:378

    Points:2694
    Re: Setting ContentType = “image/tiff” and sending an image is not working in IE

    Hi Mark,

    The aspx page had the following code to keep the page from getting cached:

    // Never cache this page
    Response.CacheControl = "no-cache";
    Response.AddHeader("Pragma", "no-cache");
    Response.Expires = -1;
    Removing the first 2 lines and leaving only Response.Expires = -1 resolved the issue. For some reason this was preventing the image from working properly in IE. 

      Modified On Apr-05-2018 04:01:49 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!