如何使用使用Response.Transmit()
问题描述:
以下是一段我的代码:如何使用使用Response.Transmit()
if (objTbl.Rows.Count > 0)
{
string attachment = "attachment; filename=Call-Details-Report-" + startDate.SelectedDate.Value.ToString("MM-dd-yyyy") + ".csv";
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "text/csv";
Response.AddHeader("Pragma", "public");
bool commaRequired = false;
if (this.chkNET_NETWORKID.Checked)
{
Response.Write("Network ID");
commaRequired = true;
}
if (this.chkNET_NETWORKNAME.Checked)
{
if (commaRequired)
{
Response.Write(",");
}
Response.Write("Network");
commaRequired = true;
}
}
在上面的代码objTbl是我的数据表。我从数据表中读取数据并将其写入响应并获取文件进行下载。但是,当我尝试下载大文件时出现错误。在执行程序期间内存不足。我听说response.transmit
。可以解决这个问题,但如何?
答
TransmitFile将指定文件直接写入HTTP响应输出流,而不将其缓冲在内存中。
这样的:
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("content-disposition", "attachment; filename=ym.jpg");
context.Response.TransmitFile(context.Server.MapPath(@"~/ym.jpg"));
context.Response.End();
多大是 “巨大的”?你在哪里得到这个错误?在网络服务器上?从客户端浏览器?从客户端应用程序? – RQDQ 2012-04-16 13:14:23
你什么时候调用Response.Transmit()方法?看起来像你的代码片丢失了一些重要的信息。 – Xharze 2012-04-16 13:14:29
@RQDQ来自客户端浏览器,文件大小约为180 mb – Jay 2012-04-16 13:20:38