我有一个 MVC 项目,它将向用户显示一些文档。这些文件当前存储在 Azure Blob 存储中。
目前,这些文档是通过以下控制器操作检索的:
[GET("{zipCode}/{loanNumber}/{classification}/{fileName}")]
public ActionResult GetDocument(string zipCode, string loanNumber, string classification, string fileName)
{
// get byte array from blob storage
byte[] doc = _docService.GetDocument(zipCode, loanNumber, classification, fileName);
string mimeType = "application/octet-stream";
return File(doc, mimeType, fileName);
}
现在,当用户单击如下链接时:
<a target="_blank" href="http://...controller//GetDocument?zipCode=84016&loanNumber=12345678classification=document&fileName=importantfile.pdf
然后,该文件将下载到浏览器的下载文件夹中。我希望发生的事情(我认为这是默认行为)是让文件简单地显示在浏览器中。
我尝试更改 mimetype 并将返回类型更改为 FileResult 而不是 ActionResult,但均无济于事。
如何让文件显示在浏览器中而不是下载?
感谢所有答案,解决方案是所有这些答案的组合。
首先,因为我使用的是byte[]
控制器的动作需要是FileContentResult
不只是FileResult
。发现这个感谢:ASP.NET MVC 中的四个文件结果有什么区别 https://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc
其次,mime 类型不能是octet-stream
。据说,使用流会导致浏览器只下载文件。我不得不改变类型application/pdf
。不过,我需要探索一个更强大的解决方案来处理其他文件/mime 类型。
第三,我必须添加一个标题来更改content-disposition
to inline
. Using 这个帖子 https://stackoverflow.com/questions/6293893/how-to-force-pdf-files-to-open-in-browser我发现我必须修改我的代码以防止重复的标头,因为内容处置已经设置为attachment
.
成功的代码:
public FileContentResult GetDocument(string zipCode, string loanNumber, string classification, string fileName)
{
byte[] doc = _docService.GetDocument(zipCode, loanNumber, classification, fileName);
string mimeType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
return File(doc, mimeType);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)