问题
I'm generating a CSV from a link on the page and sending it back to the user. I wish to maintain the page context. This code works flawlessly for IE, Chrome and Firefox on my 2008 Dev box, however when run on Vista, Win 7 or Win 8 the file has the HTML for the page appended.
I did a fair bit of reading regarding the method for delivering a file and concluded that response.flush() was the best way to go.
// Convert to Bytes for Binary Write
Byte[] bytes = Utils.Instance.ReadStreamToByte(streamWriter.BaseStream);
// Call ContentType followed by clear so no extraneous content is sent. see http://msdn.microsoft.com/en-us/library/system.web.httpresponse.aspx
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("content-length", bytes.Length.ToString());
HttpContext.Current.Response.AppendHeader("content-Disposition", "attachment;filename=" + "MyAccount Sales Report " + MonthDDL.SelectedItem.Text + "-" + YearDDL.SelectedItem.Text + ".csv");
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
I've searched for why this is occuring and are yet to find any relevant help. I did change the positioning of the Clear(); as per the Microsoft article listed in the comment above but this had no affect.
回答1:
Added Response.End() after the flush and it appears to have no adverse affects on the operations of the page post the event. A bit of a mystery still why Server 2008 behaves differently!
来源:https://stackoverflow.com/questions/15593607/response-flush-writes-html-for-page-on-windows-vista-7-8-but-not-on-server-200