How to use a common handler (.ashx) for this?
You need to add download information such as file name, contenttyp and the content itself. The pattern should give you a good hat.
public class GetDownload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (!string.IsNullOrEmpty(context.Request.QueryString["IDDownload"]))
{
context.Response.AddHeader("content-disposition", "attachment; filename=mydownload.zip");
context.Response.ContentType = "application/octet-stream";
byte[] rawBytes =
context.Response.OutputStream.Write(rawBytes, 0, rawBytes.Length);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
A common handler is called from a URL, for example:
<a href="/GetDownload.ashx?IDDownload=1337">click here to download</a>
source
share