javascript 如何从远程 URL 发送文件作为 Node.js Express 应用程序中的 GET 响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26288055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a file from remote URL as a GET response in Node.js Express app?
提问by BillyRayCyrus
Context: Multi-tier Node.js app with Express. Front end hosted as Azure website, back end data is coming from Parse.
上下文:带有 Express 的多层 Node.js 应用程序。前端托管为 Azure 网站,后端数据来自 Parse。
I have a GET endpoint and I want the user experience to be a file download. If the file lived in the same server as my front end, I could use
我有一个 GET 端点,我希望用户体验是文件下载。如果文件与我的前端位于同一台服务器中,我可以使用
res.sendfile(localPath);
However, the file is hosted by my back end on another server and all my front end knows is the URL. So I'm currently doing:
但是,该文件由我的后端托管在另一台服务器上,而我的前端只知道 URL。所以我目前正在做:
res.redirect(externalURL);
This results in the browser navigating to the asset file URL (which is a video) and it displays in the browser. What I want to know is: how to have the browser download that file instead and just stay at the same page?
这会导致浏览器导航到资产文件 URL(这是一个视频)并显示在浏览器中。我想知道的是:如何让浏览器下载该文件并停留在同一页面?
I've searched around for a while on this and I don't see a way of doing it without first downloading the file to a temporary file on my front end, which I'd obviously rather not do. This is probably HTTP 101 stuff so I appreciate any help!
我已经在这方面搜索了一段时间,但如果不先将文件下载到我前端的临时文件中,我就看不到这样做的方法,我显然宁愿不这样做。这可能是 HTTP 101 的东西,所以我感谢任何帮助!
回答by Mike S
You can use the http.request()
function to make a request to your externalURL
and then pipe()
the response back to res
. Using pipe()
will stream the file through your server to the browser, but it won't save it to disk at any point. If you want the file to be downloaded as well (as opposed to just being displayed in the browser), you'll have to set the content-disposition
header.
您可以使用该http.request()
函数向您发出请求externalURL
,然后pipe()
将响应返回给res
. 使用pipe()
将通过您的服务器将文件流式传输到浏览器,但它不会在任何时候将其保存到磁盘。如果您还希望下载文件(而不是仅显示在浏览器中),则必须设置content-disposition
header。
Here's an example of a server which will "download" the google logo. This is just using the standard http
module and not express, but it should work basically the same way:
这是将“下载”谷歌徽标的服务器示例。这只是使用标准http
模块而不是表达,但它应该以基本相同的方式工作:
var http = require('http');
http.createServer(function(req, res) {
var externalReq = http.request({
hostname: "www.google.com",
path: "/images/srpr/logo11w.png"
}, function(externalRes) {
res.setHeader("content-disposition", "attachment; filename=logo.png");
externalRes.pipe(res);
});
externalReq.end();
}).listen(8080);
If you want to use the request
module, it's even easier:
如果你想使用这个request
模块,那就更简单了:
var http = require('http'),
request = require('request');
http.createServer(function(req, res) {
res.setHeader("content-disposition", "attachment; filename=logo.png");
request('http://google.com/images/srpr/logo11w.png').pipe(res);
}).listen(8080);
Note:The name of the file that the browser will save is set by the filename=
part of the content-disposition
header; in this case I set it to logo.png
.
注意:浏览器将保存的文件名由headerfilename=
部分设置content-disposition
;在这种情况下,我将其设置为logo.png
.