我有一个指向PDF文件的链接,当我点击它时,我希望它在新标签页中呈现,而不是要求我下载它。我该怎么做?
注意,我提出这个问题是为了回答它。这些信息可以从其他答案中拼凑出来,但我希望它都在一个地方。
注意,我提出这个问题是为了回答它。这些信息可以从其他答案中拼凑出来,但我希望它都在一个地方。
要在新标签页中打开链接(PDF或其他格式),您需要修改该链接的HTML代码,将其改为:
<a href="/link_to_pdf.pdf">PDF</a>
到
<a href="/link_to_pdf.pdf" target="_blank">PDF</a>
在浏览器中打开PDF文件,您必须对响应头进行服务器端更改。在Java中,您可以这样做:
response.setContentType("application/pdf");
response.addHeader("content-disposition", "inline; filename=link_to_pdf.pdf");
关键是 inline
。如果您使用attachment
,则浏览器将尝试下载它。 您可以在这里阅读更多信息。
@GetMapping(path = "/pdf/{key}", produces = MediaType.APPLICATION_PDF_VALUE)
@ResponseBody
public InputStreamResource pdf(@PathVariable("key") String key){
InputStream file = pdfservice.get(key);
return new InputStreamResource(file);
}
inline
。对于使用 C# MVC 的人,请添加以下内容:HttpContext.Response.AddHeader("content-disposition", "inline; filename=link_to_pdf.pdf");
- SimpleGuy