简体   繁体   English

如何从 url 获取远程 PDF 并使用 Express js 发送到客户端?

[英]How to fetch remote PDF from a url and send to the client with Express js?

I would like to fetch a pdf from a URL and send it to the client in an Express (JS) application via a REST endpoint.我想从 URL 获取一个 pdf 并通过 REST 端点将它发送到 Express (JS) 应用程序中的客户端。 However, most resources are focused on sending a PDF file located on the local file system.但是,大多数资源都集中在发送位于本地文件系统上的 PDF 文件。

Here is a simple example that works great.这是一个效果很好的简单示例。

const axios = require('axios');
const express = require('express');

const app = express();

const url = 'https://example.com/file';
const fileName = 'pdf-file';

app.get('/', async (req, res) => {
  const stream = await axios.get(url, { responseType: 'stream' });
  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Content-Disposition', `inline; filename=${fileName}.pdf`);
  return stream.data.pipe(res);
});

app.listen(3000);

暂无
暂无

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

相关问题 如何从Node.js Express应用程序中将远程URL文件作为GET响应发送? - How to send a file from remote URL as a GET response in Node.js Express app? 通过Express将pdf发送到js客户端并下载 - Send pdf via express to js client and download 如何在Next js上将数据从快递服务器发送到客户端? - How to send data from express server to client on Next js? Express.js - 如何使用 res.render 从 Express 向客户端发送警报? 不重新发送 - Express.js - How to send alert from Express to Client using res.render? Not res.send 从远程URL加载pdf以作为Mandrill附件发送 - Load pdf from remote url to send as Mandrill attachment Nodejs - 从 url 获取文件并将内容发送到客户端 - Nodejs - Fetch file from url and send content to client 如何使用 Node.js express 从服务器向客户端发送实时 stream 响应 - How to send real time stream response from server to client using Node.js express 如何使用 Pusher 将用户的存款和取款详细信息从客户端发送到 Node.js(快递)服务器? - How to send deposit and withdraw details of user from client to Node.js (express) server using Pusher? 如何使用 Node.js express 从服务器向客户端发送实时响应 - How to send real time response from server to client using Node.js express 如何隐藏URL中的内容但仍然需要在node.js中发送到服务器并表达。 - How to hide content from URL but still need to send to server in node.js and express.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM