httpHandlers文件下載

1:編寫一個實現IHttpHandler的類。 

 

 public class DownloadFileProcesser : IHttpHandler
    
{
        
private DownLoadData logdata = new DownLoadData();
        
public DownloadFileProcesser()
        
{

        }


        
public void ProcessRequest(HttpContext context)
        
{
                       
string fileName = context.Request.Url.OriginalString;   //獲取請求的url
               string ORIGINALURL = fileName;  //完整的請求路徑
       int index = fileName.LastIndexOf("/");    //取最後一個/的未知
            fileName = fileName.Substring(index + 1);                 string filepath = fileName;
            
int iBegin = fileName.LastIndexOf("_");  //取"_"之前和"."後面的,組成aaa.dd
            int iEnd = fileName.LastIndexOf(".");

            
string localPath = System.Configuration.ConfigurationManager.AppSettings["downLoadPath"];  //下載文件的存放路徑
            if (iBegin >= 0)
            
{
                filepath 
= fileName.Substring(0, iBegin) + fileName.Substring(iEnd);
                filepath 
= context.Server.MapPath(localPath + filepath);
            }

            
else
            
{
                
//  filepath = Path.Combine(localPath, fileName);
                filepath = context.Server.MapPath(localPath + fileName);
            }

            
//  downLoadHandle aa = new downLoadHandle(outputBinary);
            if (File.Exists(filepath))  //文件是否存在。不存在的話按照真實文件名下載
            {
                outputBinary(fileName, filepath, context);
                sendState 
= 1;
            }

            
else
            
{
                context.Response.Status 
= "404 Not Found ";
                sendState 
= 0;
            }

            logdata.SENDSTATE 
= sendState;
           
int indexLfn = filepath.LastIndexOf("/");
            
string  localFileName = filepath.Substring(index1 + 1);
            logdata.LOCALFILENAME 
= localFileName;
            logdata.ORIGINALURL 
= ORIGINALURL;
            WriteDownLoadLog();
        }

   
// 文件按照二進制流循環輸出。其實沒必要這麼寫。
        public void outputBinary(string fileName, string filepath, HttpContext context)
        
{

            context.Response.ContentType 
= "application/octet-stream";
            context.Response.AppendHeader(
"Content-Disposition""attachment; filename=" + fileName);
            
using (System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            
{
                
byte[] aLogData = new byte[1024];
                
int len;
                
//設定每次讀取1024個字節。避免多人下載時過慢。
                while ((len = fs.Read(aLogData, 0, aLogData.Length)) > 0)
                
{
                    context.Response.BinaryWrite(_FixBuffer(aLogData, len));
                }

            }


        }

        
/// <summary>
        
/// 取指定長度的文件
        
/// </summary>
        
/// <param name="aLogData"></param>
        
/// <param name="len"></param>
        
/// <returns></returns>

        private byte[] _FixBuffer(byte[] aLogData, int len)
        
{
            
if (aLogData.Length == len)
                
return aLogData;

            
byte[] buffer = new byte[len];
            Array.Copy(aLogData, buffer, len);
            
return buffer;
        }

        
public bool IsReusable
        
{
            
get
            
{
                
return true;
            }

        }

}

 

2:配置webconfitg

<httpHandlers>

<!--當請求的後綴爲.cab的時候,調用DownloadFileProcesser這個繼承IHttpHandler接口的類-!>
   <add verb="GET,POST" path="*.cab" type="DownloadFileProcesser"/>
  </httpHandlers>

3:運行IIS服務管理器,右鍵點擊默認Web站點,選擇屬性,移動到Home目錄選項頁,並點擊配置按鈕。應用程序配置對話框彈出來了。點擊添加按鈕並在可執行字段輸入aspnet_isapi.dll文件路徑,在擴展字段輸入.kim
ps:如果你的操作系統是XP SP2的話,在輸入aspnet_isapi.dll路徑時需要手工輸入,不能用複製粘貼的形式,否則保存按鈕變灰。瀏覽時去掉check file exit選項。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章