现在的位置: 首页 > 综合 > 正文

ASP.NET 2.0 文件下载(支持大文件、防盗链)

2012年09月22日 ⁄ 综合 ⁄ 共 2764字 ⁄ 字号 评论关闭
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)
{
/*--------------------------------------------------------------*\
* 大文件下载,防盗链 *
* *
* tda7264@163.com *
* *
* 飘遥的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>

完整示例:点击下载

抱歉!评论已关闭.