using System.Web.Mvc;

namespace Models
{
    public class DownloadResult : ActionResult
    {
        public string FileName { get; set; }
        public string Path { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Buffer = true;
            context.HttpContext.Response.Clear();
            context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
            context.HttpContext.Response.ContentType = "application/unknown";   // 모든 파일 강제 다운로드
            context.HttpContext.Response.WriteFile(context.HttpContext.Server.MapPath(Path));
        }
    }
}
위와 같이 확장해서 하나 만들어주고 아래와 같이 걸어주고 호출하면 끝...

public DownloadResult FileDownload()
{
    string fileName = Request["filenames"].ToString();
    string filePath = Path.Combine(Server.MapPath("~/Downloads/"), fileName);

    return new DownloadResult
    {
	FileName = fileName,
	Path = "~/Downloads/" + fileName
    };
}
페이지에서 javascript로 location.href = 'url'; 해주어도 지가 알아서 열기,저장,취소 다이얼로그 창이 잘 떠준다 :)

익스,크롬,파폭 등 아직까진 문제가 없다 ㄷㄷㄷ

+ Recent posts