[英]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.