我正在尝试下载位于特定文件夹中的文件。我正在使用此代码,但它在Reponse.End();
- >中给出了错误无法计算表达式,因为代码已优化或本机框位于调用堆栈之上
if (m.Path.EndsWith(".txt"))
{
Response.ContentType = "application/txt";
}
else if (m.Path.EndsWith(".pdf"))
{
Response.ContentType = "application/pdf";
}
else if (m.Path.EndsWith(".docx"))
{
Response.ContentType = "application/docx";
}
else
{
Response.ContentType = "image/jpg";
}
string nameFile = m.Path;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + nameFile);
Response.TransmitFile(Server.MapPath(ConfigurationManager.AppSettings["IMAGESPATH"]) + nameFile);
Response.End();
我也试过Response.Write
,但它给了我同样的错误。
答案 0 :(得分:1)
Response.End
将抛出ThreadAbortException,它仅适用于compatibility with old ASP,您应该使用HttpApplication.CompleteRequest
这是一个例子:
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.AppendHeader("Content-Disposition", "attachment;filename=pic.jpg");
context.Response.ContentType = "image/jpg";
context.Response.TransmitFile(context.Server.MapPath("/App_Data/pic.jpg"));
context.ApplicationInstance.CompleteRequest();
}
public bool IsReusable
{
get
{
return false;
}
}
}