【发布时间】:2017-03-05 18:04:51
【问题描述】:
我的要求是单击下载链接后在新窗口/标签中打开文件。尽管按照以下帖子中的建议将内容处置设置为“内联”,但始终会下载该文件。
How to open PDF file in a new tab or window instead of downloading it (using asp.net)?
我的 HTTP 响应是
HTTP/1.1 200 OK
Cache-Control: private, s-maxage=0
Content-Type: application/octet-stream
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 5.2
content-disposition: inline;filename=FileName.pdf
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 141954
我的控制器动作如下 -
public async Task<FileContentResult> Index(string fileName)
{
byte[] document = await getFileContent("SomePathForTheFile");
HttpContext.Response.AddHeader("content-disposition", "inline;filename=" + document.FileName);
return File(document, "application/octet-stream");
}
让文件在新选项卡/窗口中自动打开而不是仅仅被下载的建议会很有帮助!谢谢!
【问题讨论】:
-
将您的 Content-Type 设置为 application/pdf
-
谢谢!这对我有用。
标签: c# asp.net-mvc-4 download