ASP.NET中实现文件的保护性下载基础篇(5)

  这里的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);
   }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjjdzp.html