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

  • 相关阅读:
    opencv3.2.0形态学滤波之腐蚀
    Ubuntu下卸载QT5.7.1再重装
    opencv3.2.0形态学滤波之膨胀
    Direct3D中的绘制
    绘制流水线
    初始化Direct3D
    VS2012添加对DirectX SDK中需要文件的引用
    ASCII,Unicode 和通用方式
    对话框访问的7种方式【孙鑫老师教程】
    函数指针
  • 原文地址:https://www.cnblogs.com/xuguanghui/p/5871906.html
Copyright © 2011-2022 走看看