REACT:如何从我的项目文件夹下载现有的 PDF 文件?

发布于 2025-01-13 21:50:59 字数 230 浏览 3 评论 0原文

我已经尝试过这样的操作:

<a    
  href="./cv.pdf"
  download
>
  download
</a>

这就是我在这种情况下所拥有的

有人可以帮助解决这个问题吗?谢谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2025-01-20 21:50:59

几分钟前,我自己也遇到了同样的问题,我找到了两种解决方案:下载文件或在新选项卡中打开它,并允许用户决定是否要下载它。

快速说明:我的文件位于项目的 /public/data 文件夹中,因为我将其视为静态资产。

  1. 下载文件:
<a
   href={process.env.PUBLIC_URL + 'data/random_file.pdf'}
   download='random_file.pdf'
>
   <button>Download Random File</button>
</a>
  1. 在新选项卡中打开文件供用户查看或下载。
<a
   href={process.env.PUBLIC_URL + 'data/random_file.pdf'}
   target='_blank'
   rel='noopener noreferrer'
>
   <button>Open Random File</button>
</a>

另外,现在提起来可能有点晚了,但我对 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.

  1. Download the file:
<a
   href={process.env.PUBLIC_URL + 'data/random_file.pdf'}
   download='random_file.pdf'
>
   <button>Download Random File</button>
</a>
  1. Open the file in a new tab for the user to view or download.
<a
   href={process.env.PUBLIC_URL + 'data/random_file.pdf'}
   target='_blank'
   rel='noopener noreferrer'
>
   <button>Open Random File</button>
</a>

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.

陌伤ぢ 2025-01-20 21:50:59

在 React.js 中下载文件
要使用 React.js 下载文件,我们可以将 download 属性添加到锚元素。

例如,我们可以这样写:

import React from "react";

export default function App() {
  return (
    <div>
      <a     href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
        download
      >
        Click to download
      </a>
    </div>
  );
}

我们只需添加 download 属性即可进行下载。

如果我们不想使用锚元素,我们还可以使用文件保护程序包来下载我们的文件。

要安装它,我们运行:

npm i file-saver

然后我们可以通过编写以下内容从包中调用 saveAs 函数:

import React from "react";
import { saveAs } from "file-saver";

export default function App() {
  const saveFile = () => { saveAs("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
      "example.pdf"
    );
  };
  return (
    <div>
      <button onClick={saveFile}>download</button>
    </div>
  );
}

第一个参数是要下载的 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:

import React from "react";

export default function App() {
  return (
    <div>
      <a     href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
        download
      >
        Click to download
      </a>
    </div>
  );
}

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:

npm i file-saver

Then we can call the saveAs function from the package by writing:

import React from "react";
import { saveAs } from "file-saver";

export default function App() {
  const saveFile = () => { saveAs("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
      "example.pdf"
    );
  };
  return (
    <div>
      <button onClick={saveFile}>download</button>
    </div>
  );
}

The first argument is the URL to download and the 2nd argument is the file name of the downloaded file.

野生奥特曼 2025-01-20 21:50:59

首先像这样导入 PDF 文件:

import cv from "./cv.pdf"

然后将其放入 href 属性中,如下所示:

<a href={cv} download="pavel_cv"> Download My CV </a>

First import your PDF file like that:

import cv from "./cv.pdf"

Then put it in the href attribute like that:

<a href={cv} download="pavel_cv"> Download My CV </a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文