Reputation:
i'm trying download files that is located in specific folder. I'm using this code, but it gives me an error in Reponse.End();
-> Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
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();
I also tried Response.Write
, but it gives me the same error.
Upvotes: 0
Views: 4260
Reputation: 20693
Response.End
will throw ThreadAbortException and it's there only for compatibility with old ASP and you should use HttpApplication.CompleteRequest
here is the example :
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;
}
}
}
Upvotes: 1