MVC 下载文件的方式

来源:互联网 发布:高干子弟的生活知乎 编辑:程序博客网 时间:2024/06/03 17:32

1:


        public void DownloadFile(string ID)
        {
            var context = HttpContext;
            string name;
            string ext;
            var filePath = _qrCodeControlService.GetFilePath(ID, out name,out ext);
            if (System.IO.File.Exists(filePath))
            {
                context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + name + "." + ext + "\"");
                context.Response.ContentType = "application/octet-stream";
                context.Response.ClearContent();
                context.Response.WriteFile(filePath);

                //return this.File(filePath, "application/octet-stream", Url.Encode(model.At_DisplayName) + "." + model.At_Extension);
            }
            else
                context.Response.StatusCode = 404;
        }

 

 

2.


        [HttpGet]
        public ActionResult ExpProjtectThird(string filePath)
        {
            var name = Path.GetFileName(filePath);
            return File(filePath, "application/vnd.ms-project", name);
        }

原创粉丝点击