下载附件后,浏览器直接打开附件

   public ActionResult Download(string dirRelativePath, string fileName)
        {
            string token = Request.QueryString["token"];
            if (htTokens != null && !string.IsNullOrEmpty(token) && htTokens.Contains(Guid.Parse(token)))
            {
                string uploadPath = System.Configuration.ConfigurationManager.AppSettings["BPMAttachments"];
                string dirAbsolutePath = uploadPath + dirRelativePath;

                if (!System.IO.File.Exists(dirAbsolutePath))
                {
                    return Content("提示:文件在磁盘上不存在");
                }
                htTokens.Remove(token);
                //HttpContext.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
                //return File(dirAbsolutePath, "application/octet-stream");
                var contentType = MimeMapping.GetMimeMapping(fileName);
                HttpContext.Response.AddHeader("content-disposition", "inline;filename=" + fileName);
                return File(dirAbsolutePath, contentType);
            }
            else
            {
                return Content("提示:没有权限");
            }
        }

注意:

1.return File(dirAbsolutePath, contentType); 中contentType不能是"application/octet-stream",需要获取文件mimetype后,指定contentType

2.浏览器支持打开的文件格式有限,例如:txt、html、png、gif。。。。。。

原文地址:https://www.cnblogs.com/xuguanghui/p/5871906.html