我在网格视图中有下载链接,当我点击它时,会弹出一个保存对话框并下载 Excel 填充。
但我收到错误“无法评估表达式,因为代码已优化或本机框架位于调用堆栈顶部。” 上Response.End()
。
代码 :
protected void grdFiles_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "download")
{
string _FileName = Convert.ToString(e.CommandArgument);
//Response.Clear();
//Response.AppendHeader("Content-Disposition", "attachment; filename=" + _FileName);
//Response.ContentType = "application//octet-stream";
//Response.TransmitFile(Server.MapPath("~/Files/" + _FileName));
//Response.End();
// Get the physical Path of the file(test.doc)
string filepath = Server.MapPath("test.doc");
// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(Server.MapPath("~/Files/" + _FileName));
// Checking if file exists
if (file.Exists)
{
// Clear the content of the response
Response.ClearContent();
// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", file.Length.ToString());
// Set the ContentType
Response.ContentType = "application/vnd.ms-excel";
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(file.FullName);
// End the response
Response.End();
}
}
}
catch (Exception ex)
{
}
}