我正在将 CSV 文件流式传输到 Internet Explorer。在流式传输结束时,Internet Explorer 会显示一个打开、保存和取消对话框。如果您在显示对话框后等待 30 秒并单击“打开”按钮,Internet Explorer 将不执行任何操作。这在 Firefox 中运行良好。除了 Flush 和 End on the Response,我还需要其他命令吗?这是我拥有的代码:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = ReportService.GetAllOwnerAircraftPaymentInformation();
//Generate the report
using (MemoryStream stream = new MemoryStream())
{
StreamWriter writer = new StreamWriter(stream);
CsvHelper csvHelper = new CsvHelper();
csvHelper.DataTableToCsv(writer, dt);
writer.Flush();
//Send it to the browser
stream.Seek(0, SeekOrigin.Begin);
string fileName = string.Format("MassAVRMSDataExport{0}.csv", DateTime.Now.ToString("yyyyMMddhhmmss"));
Response.ClearHeaders();
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
Response.AddHeader("Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture));
StreamHelper.CopyStream(stream, Response.OutputStream);
Response.Flush();
Response.End();
}
}