使用 fs.readFile 从外部 URL 获取文件

2023-11-25

我的页面上有链接,单击该链接时我想要打开外部 docx 文件。很遗憾fs.readFile只读取本地路径。

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);
   });
});

尝试这个:

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

http.get("http://www.example.com/test.docx", response => {
  response.pipe(file);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 fs.readFile 从外部 URL 获取文件 的相关文章

随机推荐