-3

我想给用户,链接到我在服务器上的文件,但我不想让他们直接链接到 asp.net C#

例如 :

http://www.DomainName.com/Files/Downloads/setup.zip

转换成

http://www.DomainName.com/Files/?filename=setup

4

1 回答 1

5

创建一个页面,DownloadMgr.aspx 例如,从查询字符串中读取文件名并将其写入响应流。

就像是:

protected void Page_Load(object sender, EventArgs e)
{
      string[] allowedExtensions = new string[] {".pdf",".zip",".txt", ".png"};
      if (!this.Page.IsPostBack)
      {
          if (Request.QueryString["File"] != null)
          {
             if (Request.QueryString["File"].Contains("pdf"))
                 Response.ContentType = "application/pdf"; //varies depending on the file being streamed
              Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"]);
              if(allowedFiles.Contains(Path.GetExtension(Request.QueryString["File"])))
                  Response.WriteFile(Server.MapPath(Request.QueryString["File"]));
           }
      }
}

现在你所有的链接都可以像:http://youhost.com/DownloadMgr.aspx?File=abc.pdf

于 2012-09-12T21:03:48.980 回答