サーバー上のファイルへのリンクをユーザーに提供したいのですが、asp.net C#との直接リンクをユーザーに提供したくありません。
例えば :
http://www.DomainName.com/Files/Downloads/setup.zip
に変換
サーバー上のファイルへのリンクをユーザーに提供したいのですが、asp.net C#との直接リンクをユーザーに提供したくありません。
例えば :
http://www.DomainName.com/Files/Downloads/setup.zip
に変換
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