Same problem over here.
The VS integrated development server works just fine. However when I deploy my project to IIS (v6.0, running on the same host) the handler isn't even called.
Instead the files are directly opened within the browser (which is expected since the HttpHandler isn't called).
Web.config:
<add verb="*" path="/download/*" type="My.NameSpace.HttpHandlerDownload" />
The Handler itself:
public class HttpHandlerDownload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
string filepath = context.Request.Url.LocalPath;
string filename = context.Request.Url.Segments[context.Request.Url.Segments.Length - 1];
context.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
context.Response.ContentType = ChooseContentTypeHeader(filename);
try
{
context.Response.WriteFile(filepath);
}
catch (System.IO.FileNotFoundException fnfex)
{
Debug.WriteLine(fnfex.Message);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
context.Response.End();
}
public bool IsReusable
{
get { return false; }
}
private static string ChooseContentTypeHeader(string filename)
{
string[] tmp = filename.Split('.');
string ext = tmp[tmp.Length - 1];
switch (ext.ToLower())
{
case "pdf":
{
return "application/pdf";
//break;
}
case "txt":
{
return "text/plain";
//break;
}
}
return null;
}
}