Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File from external URL with fs.readFile

I have links on a page that when clicked I want a external docx file to open. Unfortunately fs.readFile only reads local paths.

I tried

app.get('/getfile', function (req, res) {
   var externalURL = 'http://www.examplesite.com/example.docx';
   // var externalURL = req.query.external;
   fs.readFile(externalURL, function(err, data) {
      var fileData = new Buffer(data).toString('base64');
      res.send(fileData);
   });
});
like image 408
Oscar A Avatar asked Jul 09 '16 18:07

Oscar A


1 Answers

Try this:

const http = require("http");
const file = fs.createWriteStream("file.docx");

http.get("http://www.example.com/test.docx", response => {
  response.pipe(file);
});
like image 166
Tiago Fabre Avatar answered Sep 19 '22 03:09

Tiago Fabre