ASP.NET 2.0 文件下載(支持大文件、防盜鏈)

 1.文件下載
HTTP 文件下載主要有兩種方式:

  • URL方式直接下載,優點是:佔用服務器資源少,速度快;缺點是: 不能準確計量下載次數,無法防止盜鏈,保存在數據庫中的文件無法下載,常見格式的文件如.html 直接在瀏覽器中打開,不能直接下載。
  • 二進制數據流輸出方式,優點是:準確計量下載次數、能防盜鏈、所有文件格式都能直接下載而不是打開、保存在數據庫中等非文件數據能以文件方式下載等;缺點是佔用服務器資源多。

大文件下載原理是把文件切成小段數據流下載,微軟msdn給出了大文件下載的示例,但存在中文文件名亂碼問題,稍加改動即可。代碼爲:

protected void ResponseFile(string path)
{
System.IO.Stream iStream
= null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
string filename = System.IO.Path.GetFileName(path);

try
{
iStream
= new System.IO.FileStream(path, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead
= iStream.Length;
Response.ContentType
= "application/octet-stream";
Response.AddHeader(
"Content-Disposition", "attachment; filename=" +
HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
length
= iStream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer,
0, length);
Response.Flush();

buffer
= new Byte[10000];
dataToRead
= dataToRead - length;
}
else
{
dataToRead
= -1;
}
}
}
catch (Exception ex)
{
Response.Write(
"文件下載時出現錯誤!");
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
}

2.防止盜鏈

protected void Page_Load(object sender, EventArgs e)
{
/*--------------------------------------------------------------*/
* 大文件下載,防盜鏈 *
* *
* [email protected] *
* *
* 飄遙的BLOG
http://xianfen.net *
/* -------------------------------------------------------------
*/

if (Request.QueryString["FileName"] == null)
{
InvalidRedirect();
}

string fileName = Request.QueryString["FileName"];

if (fileName == string.Empty)
{
InvalidRedirect();
}

//判斷配置文件是否直接下載
string downDirect = ConfigurationManager.AppSettings["Down"].ToLower();

if (downDirect == "true")
{
UpdateHits(fileName);
//更新下載次數
Response.Redirect("Upload/" + fileName);
return;
}

string path = Server.MapPath(Request.ApplicationPath + "/Upload/" + fileName);
string referrer = string.Empty;

if (Request.UrlReferrer != null)
{
referrer
= Request.UrlReferrer.ToString().ToLower();
}

string d = ConfigurationManager.AppSettings["Valid"].ToLower();
string[] domainName =
ConfigurationManager.AppSettings[
"Refers"].ToLower().Split(new char[] { ',' });

// 如果設置爲防止盜鏈,判斷訪問來源是否合法
if (d == "false")
{
foreach (string s in domainName)
{
if (referrer.IndexOf(s.ToLower()) > 0)
{
UpdateHits(fileName);
//更新下載次數
ResponseFile(path);
return;
}
}

InvalidRedirect();
}
else
{
ResponseFile(path);
}
}

protected void UpdateHits(string fileName)
{
//更新文件下載次數的代碼
}

protected void InvalidRedirect()
{
string defaultPage = ConfigurationManager.AppSettings["DefaultRedirect"];

Response.Redirect(defaultPage,
true);
}


3.配置文件
配置文件中配置下載方式、盜鏈功能是否開啓及盜鏈默認轉向的頁面地址:

<appSettings>
<add key="Down" value="false"/>
<!--是否直接下載-->
<add key="Valid" value="false"/>
<!--是否允許盜鏈-->
<add key="Refers" value="localhost,google.cn"/>
<!--多個允許的訪問來源用半角的","分割-->
<add key="DefaultRedirect" value="Error.htm"/>
<!--默認轉向的頁面-->
</appSettings>
完整示例:點擊下載
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章