简体   繁体   English

在<a>标签</a>上打开本地PDF文件

[英]Open a local PDF file on <a> tag

I'm running create-react-app, and I want to open a local PDF file stored inside my repo with a relative path. 我正在运行create-react-app,我想用相对路径打开存储在我的仓库中的本地PDF文件。

<a href="../file.pdf" target="_blank">
    <button name="Click here" />
</a>

After I click it, it takes me to localhost:3000/file.pdf . 单击它后,它将带我到localhost:3000/file.pdf How do I open up a new window with a PDF? 如何打开一个带有PDF的新窗口?

You have to host your local pdf file to make it open inside your application. 您必须托管本地pdf文件,以使其在应用程序中打开。

Use SimpleHTTPServer for installing a server and run it wherever your pdf file is located. 使用SimpleHTTPServer安装服务器并在pdf文件所在的任何位置运行它。

After successfully running the SimpleHTTPServer, you can check it by opening the pdf file inside your web browser. 成功运行SimpleHTTPServer之后,可以通过在Web浏览器中打开pdf文件来进行检查。 If the pdf file is getting opened inside your current browser than hurray your file has been hosted and now it can be opened inside your application. 如果正在当前浏览器中打开pdf文件,而不是托管文件,那么现在可以在您的应用程序中打开它。

Regards 问候

Ajay 阿杰

First, let's make sure that the path to the file.pdf file is correct. 首先,让我们确保file.pdf文件的路径正确。 If file's path is true. 如果文件的路径为true。 You write on project from following code. 您从以下代码编写项目。

Actually, you should not use the button again after the tag. 实际上,您不应在标签后再次使用该按钮。 I think you edit your code by this. 我想您是以此来编辑代码的。

<a href="../file.pdf" target="_blank">
    <label>Click here</label>
</a>

or 要么

<a href="../file.pdf" target="_blank">
    Click here
</a>

Try the above code. 试试上面的代码。

您需要包括类型

<a href="../file.pdf" type="application/pdf" target="_blank">
<a href="../file.pdf" target="_blank">
    <button name="Click here" />
</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM