这是我的一段代码:
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
。可以解决这个问题但是如何?
答案 0 :(得分:1)
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();