Can't send file from Web Application on Internet Explorer
Error is something like that :
Can't load Items.aspx from 192.168.0.172
And a text is Can't open this web-site. It can't be found. Try later
code :
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.BinaryWrite(System.Text.Enco开发者_JAVA技巧ding.Unicode.GetPreamble());
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format(
"attachment; filename={0}",fileName));
....
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
Trouble with this file is only for Internet Explorer (works on opera / firefox ... )
And so it works for HTML with no
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
this string
How to avoid this error on IE ?
Try like this:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.AddHeader("Content-Type", "application/ms-excel");
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", outputFileName));
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
...
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
精彩评论