zoukankan      html  css  js  c++  java
  • 下载附件后,浏览器直接打开附件

       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。。。。。。

  • 相关阅读:
    01-节点层次
    WebAPI02
    WebAPI01
    牛客剑指Offer7
    Python字典排序
    Python字典中的键映射多个值
    计算机硬件的主要技术指标
    计算机的基本组成
    计算机系统概论
    数据库概论
  • 原文地址:https://www.cnblogs.com/xuguanghui/p/5871906.html
Copyright © 2011-2022 走看看