简体   繁体   English

在 React 中提供 PDF

[英]Serving PDFs in React

I currently have a front end React app on my localhost at port 8080 and a backend Express server on port 9000 (I think these have to be different ports but I may be wrong).我目前在我的 localhost 上的端口 8080 上有一个前端 React 应用程序,在端口 9000 上有一个后端 Express 服务器(我认为这些必须是不同的端口,但我可能错了)。 My goal is to have links on my webpage that will download PDFs (or other files like Excel sheets) to a user's local machine but I am unsure how to go about this.我的目标是在我的网页上有链接,将 PDF(或其他文件,如 Excel 表)下载到用户的本地计算机,但我不确定如何 go 关于此。

If I have the PDFs on the Express server side I thought of 2 options如果我在 Express 服务器端有 PDF,我想到了 2 个选项

  1. Create a static folder in Express which holds all the file在 Express 中创建一个 static 文件夹,其中包含所有文件
const app = express();
app.use('/static', express.static(path.join(__dirname, 'MyPDFs')));

and then user anchor tags in my React app to point to the files I need然后在我的 React 应用程序中使用用户锚标记来指向我需要的文件

<a href="http://localhost:9000/static/myFile.pdf">Link to PDF </a>
  1. Setup routes in Express and send response downloads在 Express 中设置路由并发送响应下载
var express = require("express");
var router = express.Router();

/* GET home page. */
router.get("/static", function (req, res, next) {
  res.download("MyPDFs/myFile.pdf");
});

and then make Http requests from React using fetch or the axios package like I found from this link然后使用fetchaxios package 从 React 发出 Http 请求,就像我从这个链接

axios({
  url: 'http://localhost:9000/static',
  method: 'GET',
  responseType: 'blob',
}).then((response) => {
  const url = window.URL.createObjectURL(new Blob([response.data]));
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', 'file.pdf');
  document.body.appendChild(link);
  link.click();
});

But this solution doesn't seem very clean as I have read that browsers do not natively support downloading files from requests and I need 3rd party packages to do this.但是这个解决方案似乎不是很干净,因为我读到浏览器本身不支持从请求中下载文件,我需要 3rd 方包来做到这一点。

The whole other entire option is to place these files in a directory on the front end React app but I would prefer not to do that.另一个完整的选择是将这些文件放在前端 React 应用程序的目录中,但我不希望这样做。 I would prefer to keep these PDFs (or other files like Excel) on the server side and somehow retrieve these files from React.我更愿意将这些 PDF(或 Excel 等其他文件)保留在服务器端,并以某种方式从 React 中检索这些文件。 I don't necessarily need these files to be downloaded when a user clicks a link (although thats preferable), but at least to have it display in the browser.当用户单击链接时,我不一定需要下载这些文件(尽管那是可取的),但至少要让它显示在浏览器中。

What is the "best practice / standard" approach for this situation?这种情况的“最佳实践/标准”方法是什么? Do PDF files go on the server side or client? PDF 文件 go 在服务器端还是客户端? Should I simply link to the files on the other port or send Http requests, or something else?我应该简单地链接到另一个端口上的文件还是发送 Http 请求或其他什么? Thank you in advance.先感谢您。

https://blog.logrocket.com/programmatic-file-downloads-in-the-browser-9a5186298d5c/ this may be of use to you. https://blog.logrocket.com/programmatic-file-downloads-in-the-browser-9a5186298d5c/这可能对您有用。

HTML5 introduced the download property to tags. HTML5 将下载属性引入标签。

Although not every browser will support it, you'll find you may never run into a user with an outdated browser (check the compatibility first of course).虽然不是每个浏览器都支持它,但您会发现您可能永远不会遇到使用过时浏览器的用户(当然首先检查兼容性)。

My way would be to upload the file to a storage provider and have the content downloaded from there,/CDN using an approach similar to the link.我的方法是将文件上传到存储提供商并使用类似于链接的方法从那里下载内容/CDN。 I'd use my front-end to send the file to my API, ensure it's validated/authenticated before uploading it into my storage mechanism.我会使用我的前端将文件发送到我的 API,确保在将其上传到我的存储机制之前对其进行验证/验证。

However, this introduces complexity and cost, so I'm not sure if it'd be suitable for your application.但是,这会带来复杂性和成本,因此我不确定它是否适合您的应用程序。 I certainly couldn't say if it's a standard or not.我当然不能说它是否是标准。

That also doesn't really answer your question, I'd stick to your current approach and not worry too much about the best way until your current way isn't good enough.这也不能真正回答你的问题,我会坚持你目前的方法,不要太担心最好的方法,直到你目前的方法不够好。

Also just mount your static file folder so it can be served instead of handling the route.也只需安装您的 static 文件夹,以便可以提供服务而不是处理路线。

https://expressjs.com/en/starter/static-files.html https://expressjs.com/en/starter/static-files.html

After re-reading your question, you could say this would be a combination of both approaches, downloading from the front-end, without storing your files in React.重新阅读您的问题后,您可以说这将是两种方法的组合,从前端下载,而不将文件存储在 React 中。

Finally, you don't need to make a fetch or axios request, you just need to know the location of the file, the browser will handle the downloading through the HTML5 tag, eliminating the additional network calls最后,您不需要进行 fetch 或 axios 请求,您只需要知道文件的位置,浏览器将通过 HTML5 标签处理下载,省去了额外的网络调用

TL;DR: TL;博士:

  1. Use Express with a staticly mounted directory将 Express 与静态挂载的目录一起使用
  2. Upload files from React to Express, placing in your static directory从 React 上传文件到 Express,放置在 static 目录中
  3. Use React to link the location with a download property in the Html tag使用 React 将位置链接到 Html 标签中的下载属性
  4. Download下载

When you deal with scale, I'd recommend an optional step 5:当您处理规模时,我建议您选择第 5 步:

  1. Use Express to upload content to a static storage mechanism that can be served using a CDN.使用 Express 将内容上传到可以使用 CDN 提供服务的 static 存储机制。

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

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