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)
{
/* --------------------------------------------------------------*/
* 大文件下载,防盗链 *
* *
* 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 >
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值