如果你想使下载的文件对终端的用户是私有的,需要他们通过验证用户名和密码验证访问。那么所有人都会建议你不要把文件放在web服务器的根目录下,然后建议你使用Response.TransmitFile。但是,如果你没有服务器的访问权限,对你来说实现这样的功能将会非常困难。
你不能访问服务器,你就只能就文件上传到web服务器的根目录下,这里有几个简单的步骤,能实现通过用户名和密码验证限制文件的访问。
假如说, myfile.zip 是需要通过用户名和密码验证访问的文件。
1、将myfile.zip 命名为myfile.config。
2、创建一个输入用户名和密码的页面。
3、如果用户名和密码是正确的,使用下面代码输出文件。
if
(isValidUser)
{
Response.Clear();
Response.ContentType = @" application/setup " ;
Response.AppendHeader( @" Content-Disposition " , ( " attachment; filename=myfile.zip " ));
Response.TransmitFile( @" myfile.config " );
Response.End();
}
else
{ // prompt the web user with some message of access privileges
}
{
Response.Clear();
Response.ContentType = @" application/setup " ;
Response.AppendHeader( @" Content-Disposition " , ( " attachment; filename=myfile.zip " ));
Response.TransmitFile( @" myfile.config " );
Response.End();
}
else
{ // prompt the web user with some message of access privileges
}
如果你不能访问服务器,需要使用这个诀窍。在这里唯一的解决方法是,在asp.net中不允许访问扩展名为config的文件。
本文转自麒麟博客园博客,原文链接:http://www.cnblogs.com/zhuqil/archive/2010/02/22/Limit-Access-Rights-to-download-file.html,如需转载请自行联系原作者