您的位置:首页 > 其它

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

2016-09-14 13:30 246 查看
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。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: