如何发送外部文件,只使用url(https:// .....)

aaa*_*x10 3 blob node.js express

我在尝试:

  .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);
      });
    };
Run Code Online (Sandbox Code Playgroud)

所有都适用于本地文件.你能帮忙吗?

rsp*_*rsp 7

res.sendFile将仅适用于本地文件.对于远程文件,您需要以下内容:

request('https://host.com' + attachment.req.path).pipe(res);
Run Code Online (Sandbox Code Playgroud)

使用request模块:

确保您发送正确的标头并添加一些错误处理.

另一种选择是将用户重定向到正确的URL而不是发送它:

res.redirect('https://host.com' + attachment.req.path);
Run Code Online (Sandbox Code Playgroud)

如果您希望客户端在没有服务器代理中间请求的情况下下载文件.