通过url访问iis服务器文件,ASP.NET2.0如何设置IIS下的某文件夹里面的文件不让局域网内的用户直接通过URL访问?...

看了 清清月儿 的《ASP.NET2.0雷霆之怒盗链者的祝福》的文章,按照文中的教导想实现:局域网内设置IIS下的某文件夹不让用户直接通过URL访问,而是要通过身份验证进入主页后,才能下载文件。

以下是我根据文章的指导进行操作,但最后输入 http://localhost/showbaobiao\1.rar 一样可以下载文件,而不是像文章所说的会跳转到WebForm1.aspx,在WebForm1.aspx页面中按下“下载”按钮才可以输出rar文件,请大家帮忙看看哪里出错了,谢谢!

1、首先创建一个类库项目myhandler.cs,后台的代码如下:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

///

/// myhandler 的摘要说明

///

public class myhandler:IHttpHandler

{

public myhandler()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

#region IHttpHandler 成员

public void ProcessRequest(HttpContext context)

{

// 跳转到WebForm1.aspx,由WebForm1.aspx输出rar文件

HttpResponse response = context.Response;

response.Redirect("http://192.168.1.11/showbaobiao/WebForm1.aspx");

}

public bool IsReusable

{

get

{

// TODO: 添加 MyHandler.IsReusable getter 实现

return true;

}

}

#endregion

}

2、 在配置文件Web.config文件节点里增加如下节点:

3、在WebForm1.aspx里增加一个文本为“下载”的Button,其Click事件和后台的代码如下:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

FileInfo file = new System.IO.FileInfo(Server.MapPath("1.rar"));

Response.Clear();

Response.AddHeader("Content-Disposition", "filename=" + file.Name);

Response.AddHeader("Content-Length", file.Length.ToString());

string fileExtension = file.Extension;

// 根据文件后缀指定文件的Mime类型

switch (fileExtension)

{

case "mp3":

Response.ContentType = "audio/mpeg3";

break;

case "mpeg":

Response.ContentType = "video/mpeg";

break;

case "jpg":

Response.ContentType = "image/jpeg";

break;

case "bmp":

Response.ContentType = "image/bmp";

break;

case "gif":

Response.ContentType = "image/gif";

break;

case "doc":

Response.ContentType = "application/msword";

break;

case "css":

Response.ContentType = "text/css";

break;

default:

Response.ContentType = "application/octet-stream";

break;

}

Response.WriteFile(file.FullName);

Response.End();

}

}

4、 最后一步就是在IIS里增加一个应用程序扩展。在“默认网站”->“属性”->“主目录”->“配置”。在弹出的“应用程序配置”窗口里按“添加”,在弹出的“添加/编辑应用程序扩展名映射”窗口里“可执行文件”选择C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,在扩展名里输入“.rar”,然后确定即可。

5、 在IE里输入http://localhost/shobaobiao/1.rar,会立即跳转到http://localhost/shobaobiao/WebForm1.aspx,然后按WebForm1.aspx的“下载”按钮就可以下载1.rar了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值