在浏览器的新标签页中打开PDF [英] Open PDF in a new tab in browser

查看:327
本文介绍了在浏览器的新标签页中打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前从未做过此操作,因此不确定其中涉及什么.我进行了搜索,发现了许多答案,但它们比我需要的要复杂得多.例如,他们需要放大,生成,创建准确的缩略图,在网页中嵌入实际的PDF等.但是我的问题要简单得多: 如果我要在网页上显示其信息的家伙要显示一些PDF,而我只想在页面上放一个通用的PDF图标,人们就可以单击它,而实际的PDF会在浏览器的新标签页中打开.

Never done this before so not sure what is involved in it. I did search and found many answers but they were more complex than what I need. For example they needed to zoom in, generate, create accurate thumbnail, embed the actual PDF in the webpage, etc... But my question is much simpler: If my guy that I am showing his information on webpage has some PDF to show I just want to put a generic PDF icon on the page, people click on it and the actual PDF opens in a new tab in their browser.

这样做会涉及到什么? 它不像文件路径,PDF作为二进制对象保存在SQL Server中,或者将其保存在SQL Server中.它不是服务器上的文件磁盘路径

What is involved in just doing that? It is not like a file path, the PDF is saved in SQL server as binary objects or whatever it does to save it in SQL Server..it is not a file disk path on server

推荐答案

您的标记表示asp.net-mvc.

Your tags indicate asp.net-mvc.

创建一个控制器来处理对PDF文件的请求

Create a controller to handle requests for the PDF file

伪:

[RoutePrefix("Pdf")]
public class PdfController : Controller {
    [Route("{id}"]
    public ActionResult GetPDF(int id) {    
        //...Code to extract pdf from SQLServer and store in stream
        Stream stream = GetDataFromSQLServerById(id);
        return File(stream,"filename.pdf");
    }
}

在客户端上

<a href="/Pdf/123456" target="_blank">
    <img src="images/pdficon.jpg">
</a>

更新:

引用@ChrisPratt的评论; (我忘记将其包含在答案中)

Referencing @ChrisPratt's comment; (which I forgot to include in my answer)

anchor标签上的target属性将告诉浏览器在新标签页中打开链接.

The target attribute on the anchor tag is what will tell the browser to open the link in a new tab.

这篇关于在浏览器的新标签页中打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆