Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send external file, use just url(https://.....)

i am trying:

  .post("/getAttachments", (req, res, next) => {
    repository.getAttachments(req.body)
      .then((attachment) => {
        return res.sendFile('https://host.com' + attachment.req.path);            
      })
      .catch(next);
  })
///clientService:
 function getAttachments(params) {
      return $http.post(baseUrl + "/getAttachments", params, {responseType: 'arraybuffer'}).success(function (data) {
        let blob  = new Blob([data], {type: 'application/vnd.ms-excel'});
        saveAs(blob);
      });
    };

all works for local files only. Please Can you help with it?

like image 646
aaaaaaaaax10 Avatar asked Feb 04 '23 23:02

aaaaaaaaax10


1 Answers

res.sendFile will work only for local files. For remote file you would need something like:

request('https://host.com' + attachment.req.path).pipe(res);

using request module:

  • https://github.com/request/request

Make sure that you send correct headers and add some error handling.

Another option would be to redirect the user to the correct URL instead of sending it:

res.redirect('https://host.com' + attachment.req.path);

if you want the client to download the file without your server proxying the request in the middle.

like image 185
rsp Avatar answered Feb 08 '23 16:02

rsp