Content-Disposition Doesn't Set Filename in IE

Discussion in 'ASP .Net' started by =?Utf-8?B?SmVk?=, Apr 6, 2005.

  1. I have looked everywhere and cannot find a resolution for this. I have read
    several post on this forum that allude to similar issues but I have not found
    a solution.

    I am sending dynamic content to the browser to be opened in Excel. On some
    browsers including on the mac it works fine, but in other browsers it sets
    the filename to the script instead of to the name specified in the
    content-disposition.

    I am at my wits end. Here is a snapshot of my code, but I have tried dozens
    of permutations. It works on my local machine but not on the server. The
    server is running under HTTPS which may be an issue? I have tried without
    and with the mime type defined in the IIS on the server. The request is
    launched from a javascript window.open and I have tried both relative and
    absolute urls.

    FYI: this is part of a function and the context is the current httpcontext.
    I am doing all this from an HttpHandler with session state enabled.

    XslTransform xslt = new XslTransform();
    xslt.Load(xslPath);
    System.IO.MemoryStream stream = new System.IO.MemoryStream();
    xslt.Transform(xmlDocument, xargs, stream, new XmlUrlResolver());
    context.Response.Buffer = true;
    context.Response.Clear();
    context.Response.ClearContent();
    context.Response.ClearHeaders();
    context.Response.Cookies.Clear();
    context.Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    //string.Empty;
    context.Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    context.Response.AppendHeader("Content-Length", stream.Length.ToString());
    //context.Response.AppendHeader("Content-Disposition-Type","inline");
    //context.Response.AppendHeader("Pragma","cache");
    //context.Response.AppendHeader("Expires", "-1");
    //context.Response.AppendHeader("Cache-Control","no-store, no-cache,
    must-revalidate, post-check=0, pre-check=0");
    context.Response.ContentType = "application/vnd.ms-excel";
    context.Response.AppendHeader("Content-Disposition",
    "inline; " +
    "filename=\"" + "Download.xls" + "\"; " +
    "size=" + stream.Length.ToString() + "; " +
    "creation-date=" + DateTime.Now.ToString("R") + "; " +
    "modification-date=" + DateTime.Now.ToString("R") + "; " +
    "read-date=" + DateTime.Now.ToString("R"));
    context.Response.BinaryWrite(stream.ToArray());
    stream.Close();
    context.Response.Flush();
    context.Response.Close();
    stream = null;
    context.Response.End();
     
    =?Utf-8?B?SmVk?=, Apr 6, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?SmVk?=

    Bruce Barker Guest

    well its up to the browser to honor the content file name. as you found,
    many don't. you have two options:

    1) live with it.
    2) use an http module, and use url remapping to get around the problem, so
    that

    Download.xls get remapped to producexml.aspx


    -- bruce (sqlwork.com)

    ..



    "Jed" <> wrote in message
    news:...
    >I have looked everywhere and cannot find a resolution for this. I have
    >read
    > several post on this forum that allude to similar issues but I have not
    > found
    > a solution.
    >
    > I am sending dynamic content to the browser to be opened in Excel. On
    > some
    > browsers including on the mac it works fine, but in other browsers it sets
    > the filename to the script instead of to the name specified in the
    > content-disposition.
    >
    > I am at my wits end. Here is a snapshot of my code, but I have tried
    > dozens
    > of permutations. It works on my local machine but not on the server. The
    > server is running under HTTPS which may be an issue? I have tried without
    > and with the mime type defined in the IIS on the server. The request is
    > launched from a javascript window.open and I have tried both relative and
    > absolute urls.
    >
    > FYI: this is part of a function and the context is the current
    > httpcontext.
    > I am doing all this from an HttpHandler with session state enabled.
    >
    > XslTransform xslt = new XslTransform();
    > xslt.Load(xslPath);
    > System.IO.MemoryStream stream = new System.IO.MemoryStream();
    > xslt.Transform(xmlDocument, xargs, stream, new XmlUrlResolver());
    > context.Response.Buffer = true;
    > context.Response.Clear();
    > context.Response.ClearContent();
    > context.Response.ClearHeaders();
    > context.Response.Cookies.Clear();
    > context.Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    > //string.Empty;
    > context.Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    > context.Response.AppendHeader("Content-Length", stream.Length.ToString());
    > //context.Response.AppendHeader("Content-Disposition-Type","inline");
    > //context.Response.AppendHeader("Pragma","cache");
    > //context.Response.AppendHeader("Expires", "-1");
    > //context.Response.AppendHeader("Cache-Control","no-store, no-cache,
    > must-revalidate, post-check=0, pre-check=0");
    > context.Response.ContentType = "application/vnd.ms-excel";
    > context.Response.AppendHeader("Content-Disposition",
    > "inline; " +
    > "filename=\"" + "Download.xls" + "\"; " +
    > "size=" + stream.Length.ToString() + "; " +
    > "creation-date=" + DateTime.Now.ToString("R") + "; " +
    > "modification-date=" + DateTime.Now.ToString("R") + "; " +
    > "read-date=" + DateTime.Now.ToString("R"));
    > context.Response.BinaryWrite(stream.ToArray());
    > stream.Close();
    > context.Response.Flush();
    > context.Response.Close();
    > stream = null;
    > context.Response.End();
    >
     
    Bruce Barker, Apr 6, 2005
    #2
    1. Advertising

  3. Thanks, Bruce,

    Maybe that is what I should do. Can you explain why it works in my local
    development environment but not on the live server?

    Obviously, the browser is the same in both contexts.

    Thanks again.

    "Bruce Barker" wrote:

    > well its up to the browser to honor the content file name. as you found,
    > many don't. you have two options:
    >
    > 1) live with it.
    > 2) use an http module, and use url remapping to get around the problem, so
    > that
    >
    > Download.xls get remapped to producexml.aspx
    >
    >
    > -- bruce (sqlwork.com)
    >
    > ..
    >
    >
    >
    > "Jed" <> wrote in message
    > news:...
    > >I have looked everywhere and cannot find a resolution for this. I have
    > >read
    > > several post on this forum that allude to similar issues but I have not
    > > found
    > > a solution.
    > >
    > > I am sending dynamic content to the browser to be opened in Excel. On
    > > some
    > > browsers including on the mac it works fine, but in other browsers it sets
    > > the filename to the script instead of to the name specified in the
    > > content-disposition.
    > >
    > > I am at my wits end. Here is a snapshot of my code, but I have tried
    > > dozens
    > > of permutations. It works on my local machine but not on the server. The
    > > server is running under HTTPS which may be an issue? I have tried without
    > > and with the mime type defined in the IIS on the server. The request is
    > > launched from a javascript window.open and I have tried both relative and
    > > absolute urls.
    > >
    > > FYI: this is part of a function and the context is the current
    > > httpcontext.
    > > I am doing all this from an HttpHandler with session state enabled.
    > >
    > > XslTransform xslt = new XslTransform();
    > > xslt.Load(xslPath);
    > > System.IO.MemoryStream stream = new System.IO.MemoryStream();
    > > xslt.Transform(xmlDocument, xargs, stream, new XmlUrlResolver());
    > > context.Response.Buffer = true;
    > > context.Response.Clear();
    > > context.Response.ClearContent();
    > > context.Response.ClearHeaders();
    > > context.Response.Cookies.Clear();
    > > context.Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    > > //string.Empty;
    > > context.Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    > > context.Response.AppendHeader("Content-Length", stream.Length.ToString());
    > > //context.Response.AppendHeader("Content-Disposition-Type","inline");
    > > //context.Response.AppendHeader("Pragma","cache");
    > > //context.Response.AppendHeader("Expires", "-1");
    > > //context.Response.AppendHeader("Cache-Control","no-store, no-cache,
    > > must-revalidate, post-check=0, pre-check=0");
    > > context.Response.ContentType = "application/vnd.ms-excel";
    > > context.Response.AppendHeader("Content-Disposition",
    > > "inline; " +
    > > "filename=\"" + "Download.xls" + "\"; " +
    > > "size=" + stream.Length.ToString() + "; " +
    > > "creation-date=" + DateTime.Now.ToString("R") + "; " +
    > > "modification-date=" + DateTime.Now.ToString("R") + "; " +
    > > "read-date=" + DateTime.Now.ToString("R"));
    > > context.Response.BinaryWrite(stream.ToArray());
    > > stream.Close();
    > > context.Response.Flush();
    > > context.Response.Close();
    > > stream = null;
    > > context.Response.End();
    > >

    >
    >
    >
     
    =?Utf-8?B?SmVk?=, Apr 7, 2005
    #3
  4. OK,

    I went with the extension mapping option and it doesn't work.

    I mapped the xls extension and am handling it with an HttpHandler. It
    behaves exactly the same as running it through an aspx. It works locally
    where there is no SSL but it doesn't work on the server where the request is
    secured. So even though the "file" has the xls extension, it still won't
    download.

    I don't think it is an MSOffice issue, I think it is a security issue. I
    have seen unanswered posts about Cacheability having an effect, but I have
    seen no answers on the subject.

    I am stumped.

    "Bruce Barker" wrote:

    > well its up to the browser to honor the content file name. as you found,
    > many don't. you have two options:
    >
    > 1) live with it.
    > 2) use an http module, and use url remapping to get around the problem, so
    > that
    >
    > Download.xls get remapped to producexml.aspx
    >
    >
    > -- bruce (sqlwork.com)
    >
    > ..
    >
    >
    >
    > "Jed" <> wrote in message
    > news:...
    > >I have looked everywhere and cannot find a resolution for this. I have
    > >read
    > > several post on this forum that allude to similar issues but I have not
    > > found
    > > a solution.
    > >
    > > I am sending dynamic content to the browser to be opened in Excel. On
    > > some
    > > browsers including on the mac it works fine, but in other browsers it sets
    > > the filename to the script instead of to the name specified in the
    > > content-disposition.
    > >
    > > I am at my wits end. Here is a snapshot of my code, but I have tried
    > > dozens
    > > of permutations. It works on my local machine but not on the server. The
    > > server is running under HTTPS which may be an issue? I have tried without
    > > and with the mime type defined in the IIS on the server. The request is
    > > launched from a javascript window.open and I have tried both relative and
    > > absolute urls.
    > >
    > > FYI: this is part of a function and the context is the current
    > > httpcontext.
    > > I am doing all this from an HttpHandler with session state enabled.
    > >
    > > XslTransform xslt = new XslTransform();
    > > xslt.Load(xslPath);
    > > System.IO.MemoryStream stream = new System.IO.MemoryStream();
    > > xslt.Transform(xmlDocument, xargs, stream, new XmlUrlResolver());
    > > context.Response.Buffer = true;
    > > context.Response.Clear();
    > > context.Response.ClearContent();
    > > context.Response.ClearHeaders();
    > > context.Response.Cookies.Clear();
    > > context.Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    > > //string.Empty;
    > > context.Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    > > context.Response.AppendHeader("Content-Length", stream.Length.ToString());
    > > //context.Response.AppendHeader("Content-Disposition-Type","inline");
    > > //context.Response.AppendHeader("Pragma","cache");
    > > //context.Response.AppendHeader("Expires", "-1");
    > > //context.Response.AppendHeader("Cache-Control","no-store, no-cache,
    > > must-revalidate, post-check=0, pre-check=0");
    > > context.Response.ContentType = "application/vnd.ms-excel";
    > > context.Response.AppendHeader("Content-Disposition",
    > > "inline; " +
    > > "filename=\"" + "Download.xls" + "\"; " +
    > > "size=" + stream.Length.ToString() + "; " +
    > > "creation-date=" + DateTime.Now.ToString("R") + "; " +
    > > "modification-date=" + DateTime.Now.ToString("R") + "; " +
    > > "read-date=" + DateTime.Now.ToString("R"));
    > > context.Response.BinaryWrite(stream.ToArray());
    > > stream.Close();
    > > context.Response.Flush();
    > > context.Response.Close();
    > > stream = null;
    > > context.Response.End();
    > >

    >
    >
    >
     
    =?Utf-8?B?SmVk?=, Apr 8, 2005
    #4
  5. Well, I think I have it resolved, though I am not sure what of the following
    fixed it.

    Here are a couple useful resource links:
    http://support.microsoft.com/?kbid=317208
    http://www.generation.net/~hleboeuf/downfile.htm

    Here is the header code:
    context.Response.Clear();
    context.Response.ClearContent();
    context.Response.ClearHeaders();
    context.Response.Cookies.Clear();
    context.Response.Cache.SetCacheability(HttpCacheability.Private);
    context.Response.CacheControl = "private";
    context.Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    context.Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    context.Response.AppendHeader("Content-Length", stream.Length.ToString());
    context.Response.AppendHeader("Pragma","cache");
    context.Response.AppendHeader("Expires", "60");
    context.Response.ContentType = "application/vnd.ms-excel";
    context.Response.AppendHeader("Content-Disposition",
    "inline; " +
    "filename=\"" + fileName + "\"; " +
    "size=" + stream.Length.ToString() + "; " +
    "creation-date=" + DateTime.Now.ToString("R") + "; " +
    "modification-date=" + DateTime.Now.ToString("R") + "; " +
    "read-date=" + DateTime.Now.ToString("R"));

    According to the KB article above, Office cannot open files that have not
    been cached by IE. I changed the cache settings to accommodate this
    "limitation". Though the following may have been enough.

    One of the Advanced settings in the IE options is "Do not save encrypted
    pages to disk" I had this option checked and I believe this is why neither
    download (attachment) nor open (inline) worked for me in HTTPS/SSL but did
    work for others.
     
    =?Utf-8?B?SmVk?=, Apr 8, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Costas Andriotis
    Replies:
    0
    Views:
    1,154
    Costas Andriotis
    Oct 15, 2004
  2. Carlos Henrique
    Replies:
    0
    Views:
    462
    Carlos Henrique
    Aug 18, 2003
  3. Ed
    Replies:
    10
    Views:
    45,966
    alok000707
    Jul 13, 2010
  4. Stefano Crocco
    Replies:
    0
    Views:
    148
    Stefano Crocco
    Nov 14, 2008
  5. Diarmaid
    Replies:
    3
    Views:
    187
    Evertjan.
    May 12, 2004
Loading...

Share This Page