REACT:如何从我的项目文件夹下载现有的 PDF 文件?
I have tried it like so:
<a
href="./cv.pdf"
download
>
download
</a>
That's what I have in this case
Can someone help with the solution? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几分钟前,我自己也遇到了同样的问题,我找到了两种解决方案:下载文件或在新选项卡中打开它,并允许用户决定是否要下载它。
快速说明:我的文件位于项目的
/public/data
文件夹中,因为我将其视为静态资产。另外,现在提起来可能有点晚了,但我对 Web 开发和 React 非常陌生,所以请对这个信息持保留态度。我希望这个解决方案也适合您,或者至少为您指明了正确的方向。
I just had the same issue myself, literally a few minutes ago, and I found two solutions to this: Download the file or open it in a new tab and allow the user to decide if they want to download it or not.
A quick note: the file I have is located in the
/public/data
folder of my project, since I treat it as a static asset.Also, it might be a bit late to mention but I am extremely new to web development and React so please take this info with a grain of salt. And I hope this solution works for you too, or at least points you in the right direction.
在 React.js 中下载文件
要使用 React.js 下载文件,我们可以将 download 属性添加到锚元素。
例如,我们可以这样写:
我们只需添加 download 属性即可进行下载。
如果我们不想使用锚元素,我们还可以使用文件保护程序包来下载我们的文件。
要安装它,我们运行:
然后我们可以通过编写以下内容从包中调用 saveAs 函数:
第一个参数是要下载的 URL,第二个参数是下载文件的文件名。
Download File in React.js
To download a file with React.js, we can add the download attribute to an anchor element.
For instance, we can write:
We just add the download prop to do the download.
If we don’t want to use an anchor element, we can also use the file-saver package to download our file.
To install it, we run:
Then we can call the saveAs function from the package by writing:
The first argument is the URL to download and the 2nd argument is the file name of the downloaded file.
首先像这样导入 PDF 文件:
然后将其放入 href 属性中,如下所示:
First import your PDF file like that:
Then put it in the href attribute like that: