这里的ProcessRequest方法会调用StartDownload方法,完整的Download.ashx代码如下所示:
Code highlighting produced by Actipro CodeHighlighter (freeware)
<%@ WebHandler Language="C#" Class="Download"%>
using System;
using System.Web;
publicclass Download : IHttpHandler
{
publicvoid ProcessRequest (HttpContext context)
{
if (context.User.Identity.IsAuthenticated)
{
if(context.Request.QueryString["Product"] !=null
&& context.Request.QueryString["Product"] !="")
{
int productID = Convert.ToInt16(
context.Request.QueryString["Product"]);
string userName = context.User.Identity.Name;
UserProduct product = UserProductFactory.GetProductByUser(
userName, productID);
if(product !=null)
StartDownload(product.FileName);
else
context.Response.Redirect(
"~/Downloads/Files/AccessDenied.aspx");
}
}
}
publicbool IsReusable
{
get
{
returnfalse;
}
}
privatevoid StartDownload(string downloadFile)
{
context.Response.Buffer =true;
context.Response.Clear();
context.Response.AddHeader(
"content-disposition",
"attachment; filename="+ downloadFile);
context.Response.ContentType ="application/zip";
context.Response.WriteFile(
"~/Downloads/Files/"+ downloadFile);
}
}