Reputation: 303
I want to download a pdf file when clicking a button on a html page.The html page is deployed on nodejs.And the pdf file is on apache http server.So the html page and nodejs server are on the same domain, but the html page and apache http server are cross domain.
Can I do like this?
when I click the button, I send a request to nodejs, and nodejs get file from apache http server, and then send response to client(browser).Can this case work?
And if the pdf file is very large, I can wait until nodejs get it over and then send response to client.I just want to achieve this goal, download this file like <a href="xxx" download="yyy">Download<a>
Can I achieve this goal?
Upvotes: 0
Views: 298
Reputation: 2603
Yes, you can do that. Browser -> Node.JS Server -> Http Server -> Node.JS Server -> Browser
Using Request module you can stream files between req and res. https://www.npmjs.com/package/request#streaming
Example:
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp)
}
}).listen(port);
Upvotes: 0