1.文件下载
HTTP 文件下载主要有两种方式:

URL方式直接下载,优点是:占用服务器资源少,速度快;缺点是: 不能准确计量下载次数,无法防止盗链,保存在数据库中的文件无法下载,常见格式的文件如.html 直接在浏览器中打开,不能直接下载。
二进制数据流输出方式,优点是:准确计量下载次数、能防盗链、所有文件格式都能直接下载而不是打开、保存在数据库中等非文件数据能以文件方式下载等;缺点是占用服务器资源多。
大文件下载原理是把文件切成小段数据流下载,微软msdn给出了大文件下载的示例,但存在中文文件名乱码问题,稍加改动即可。代码为:
InBlock.gif protected void ResponseFile( string path) {    
InBlock.gif        System.IO.Stream iStream = null;    
InBlock.gif         byte[] buffer = new Byte[10000];    
InBlock.gif         int length;    
InBlock.gif         long dataToRead;    
InBlock.gif         string filename = System.IO.Path.GetFileName(path);    
InBlock.gif    
InBlock.gif         try {    
InBlock.gif                iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);    
InBlock.gif                dataToRead = iStream.Length;    
InBlock.gif                Response.ContentType = "application/octet-stream";    
InBlock.gif                Response.AddHeader( "Content-Disposition", "p_w_upload; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));    
InBlock.gif                 while (dataToRead > 0) {    
InBlock.gif                         if (Response.IsClientConnected) {    
InBlock.gif                                length = iStream.Read(buffer, 0, 10000);    
InBlock.gif                                Response.OutputStream.Write(buffer, 0, length);    
InBlock.gif                                Response.Flush();    
InBlock.gif                                buffer = new Byte[10000];    
InBlock.gif                                dataToRead = dataToRead - length;    
InBlock.gif                                }    
InBlock.gif                         else {    
InBlock.gif                                dataToRead = -1;    
InBlock.gif                                }    
InBlock.gif                        }    
InBlock.gif                }    
InBlock.gif         catch (Exception ex) {    
InBlock.gif                Response.Write( "文件下载时出现错误!");    
InBlock.gif                }    
InBlock.gif                 finally {    
InBlock.gif                         if (iStream != null) {    
InBlock.gif                                iStream.Close();    
InBlock.gif                                }    
InBlock.gif                        }    
InBlock.gif                }
2.防止盗链
InBlock.gif protected void Page_Load( object sender, EventArgs e) {    
InBlock.gif         /*-------------大文件下载,防盗链------------------*/
InBlock.gif         if (Request.QueryString[ "FileName"] == null) { InvalidRedirect(); }    
InBlock.gif         string fileName = Request.QueryString[ "FileName"];    
InBlock.gif         if (fileName == string.Empty) { InvalidRedirect(); }    
InBlock.gif         //判断配置文件是否直接下载    
InBlock.gif         string downDirect = ConfigurationManager.AppSettings[ "Down"].ToLower();    
InBlock.gif         if (downDirect == "true") { UpdateHits(fileName);    
InBlock.gif         //更新下载次数 Response.Redirect("Upload/" + fileName);    return; }    
InBlock.gif         string path = Server.MapPath(Request.ApplicationPath + "/Upload/" + fileName);    
InBlock.gif         string referrer = string.Empty;    
InBlock.gif         if (Request.UrlReferrer != null) { referrer = Request.UrlReferrer.ToString().ToLower(); }    
InBlock.gif         string d = ConfigurationManager.AppSettings[ "Valid"].ToLower();    
InBlock.gif         string[] domainName = ConfigurationManager.AppSettings[ "Refers"].ToLower().Split( new char[] { ',' });    
InBlock.gif         // 如果设置为防止盗链,判断访问来源是否合法    
InBlock.gif         if (d == "false") {    
InBlock.gif                 foreach ( string s in domainName) {    
InBlock.gif                         if (referrer.IndexOf(s.ToLower()) > 0) { UpdateHits(fileName);    
InBlock.gif                         //更新下载次数    
InBlock.gif                        ResponseFile(path);    
InBlock.gif                         return;    
InBlock.gif                        }    
InBlock.gif                        }    
InBlock.gif                InvalidRedirect();    
InBlock.gif                }    
InBlock.gif                 else {    
InBlock.gif                        ResponseFile(path);    
InBlock.gif                        }    
InBlock.gif                }    
InBlock.gif         protected void UpdateHits( string fileName) {    
InBlock.gif                 //更新文件下载次数的代码    
InBlock.gif                }    
InBlock.gif         protected void InvalidRedirect() {    
InBlock.gif                 string defaultPage = ConfigurationManager.AppSettings[ "DefaultRedirect"];    
InBlock.gif                Response.Redirect(defaultPage, true);    
InBlock.gif                }
3.配置文件
配置文件中配置下载方式、盗链功能是否开启及盗链默认转向的页面地址:
InBlock.gif<appSettings>    
InBlock.gif        <add key= "Down" value= "false"/>    
InBlock.gif        <!--是否直接下载-->    
InBlock.gif        <add key= "Valid" value= "false"/>    
InBlock.gif        <!--是否允许盗链-->    
InBlock.gif        <add key= "Refers" value= "localhost,google.cn"/>    
InBlock.gif        <!--多个允许的访问来源用半角的 ","分割-->    
InBlock.gif        <add key= "DefaultRedirect" value= "Error.htm"/>    
InBlock.gif        <!--默认转向的页面-->    
InBlock.gif</appSettings>