-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 に答える