javascript - 如何让浏览器在新选项卡中打开 pdf 而不仅仅是下载它?

标签 javascript asp.net-mvc

我有一个“下载”按钮和一个“查看”按钮。文件是通过返回 FileResult 的 MVC Controller 函数下载的。

Controller 功能如下所示:

    public FileResult DownloadStatement(Guid statementID) {
        StatementFileModel statementFile = BL.GetStatementByID(statementID);
        byte[] fileBytes = statementFile.FileData;
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + statementFile.FileName);
        Response.BinaryWrite(fileBytes);
        return File(fileBytes, "application/pdf");
    }

以下方法非常适合立即下载 PDF,而无需在浏览器中打开它:

window.location.href = "/DownloadStatement?statementID=" + statementID;

不过,我在使用“查看”按钮时遇到了问题。我尝试了以下操作,但它也只是下载 PDF,而不在浏览器中打开它:

window.open("/DownloadStatement?statementID=" + statementID, '_blank', 'fullscreen=yes');

最佳答案

Content-Disposition:attachment header 告诉浏览器下载文件。单击“查看”按钮时不要发送该 header 值(附件),浏览器应该显示 PDF 而不是下载它。

完全省略 header ,或发送 Content-Disposition: inline 就足够了。

关于javascript - 如何让浏览器在新选项卡中打开 pdf 而不仅仅是下载它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682712/

相关文章:

javascript - Rxjs/reactor limit operator 等效于 ajax 请求

javascript - 获取文本框值并放入 HREF

javascript - option 和 jQuery .click() 不能一起工作

c# - SignalR 2、Autofac 3.5、MVC 5 和 OWIN : How to inject SignalR hub into a controller?

c# - Ajax.BeginForm 绝对 url MVC CORS

javascript - Nodejs异步 waterfall 在第二个函数中使用mongoose findOneAndUpdate

javascript - 使用 :contains to found a text jquery

asp.net-mvc - 如何在应用程序中支持多个客户?

c# - 如何从 Asp .Net 中的 Active Directory 获取不同的部门名称?

c# - 用C#读取Atom xml