此代码在其他页面的PageLoad函数中有效,我只是重定向到它。但是当我将它保留在同一页面时,它会抛出错误 -
无法评估表达式,因为代码已优化或本机框位于调用堆栈之上。
protected void Download(object sender, CommandEventArgs e)
{
//Response.Redirect("/Forms/Download.aspx?file="+sFile, false);
string sFile = "~/Attachments"+e.CommandArgument.ToString();
If(File.Exists(Server.MapPath(sFile)))
{
string Path = Server.MapPath(sFile));
FileInfo file = new FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename="
+file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.Flush();
Response.WriteFile(file.FullName);
Response.End();
}
}
}
我上次收到此错误时,有人建议添加“false' false'到endResponse。但不知道在这里做什么。
答案 0 :(得分:0)
return File(document.Data, document.ContentType, document.Name);
这将在返回时为用户提供下载提示。
答案 1 :(得分:0)
请尝试以下代码序列:
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename="
+file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
Response.End();