简体   繁体   中英

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.

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

After I click it, it takes me to localhost:3000/file.pdf . How do I open up a new window with a PDF?

You have to host your local pdf file to make it open inside your application.

Use SimpleHTTPServer for installing a server and run it wherever your pdf file is located.

After successfully running the SimpleHTTPServer, you can check it by opening the pdf file inside your web browser. 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.

Regards

Ajay

First, let's make sure that the path to the file.pdf file is correct. If file's path is 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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