Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

javascript - How to send a file from remote URL as a GET response in Node.js Express app?

Context: Multi-tier Node.js app with Express. Front end hosted as Azure website, back end data is coming from 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

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:

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?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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:

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...