ファイル(ASHX)をダウンロードするための一般的なハンドラを作成します。
ProcessRequest
メソッドでは、セッションを確認してファイルを提供できます。次に例を示します。
public class Download : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string file = "setup.exe";
if (File.Exists(context.Server.MapPath(file)) && Session["scr"].ToString() == "Ok")
{
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(file));
context.Response.WriteFile(context.Server.MapPath(file));
context.Response.End();
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("File cannot be found!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
誰もが直接、それをダウンロードすることはできませんので、あなたのIISでのexeファイルのMIMEを削除することを忘れないでください。