【问题标题】:Response.Flush writes HTML for page on Windows Vista, 7, 8 but not on Server 2008Response.Flush 在 Windows Vista、7、8 上为页面写入 HTML,但在 Server 2008 上不写入
【发布时间】:2013-03-24 00:13:18
【问题描述】:

我正在从页面上的链接生成 CSV 并将其发送回用户。我希望维护页面上下文。此代码在我的 2008 Dev box 上完美运行 IE、Chrome 和 Firefox,但是在 Vista、Win 7 或 Win 8 上运行时,该文件附加了页面的 HTML。

我对传递文件的方法做了相当多的阅读,并得出结论,response.flush() 是最好的方法。

        // 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();

我已经搜索了发生这种情况的原因,但尚未找到任何相关帮助。我确实改变了 Clear(); 的位置;根据上面评论中列出的 Microsoft 文章,但这没有影响。

【问题讨论】:

    标签: asp.net html download response flush


    【解决方案1】:

    在刷新后添加了 Response.End() ,它似乎对事件发布后页面的操作没有不利影响。为什么 Server 2008 的行为不同,仍然有点谜!

    【讨论】:

      猜你喜欢
      • 2010-12-14
      • 2011-01-14
      • 2010-10-17
      • 2019-02-04
      • 2023-03-12
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多