我有一个“下载”按钮和一个“查看”按钮。文件是通过返回 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/