使用fs.readFile从外部URL获取文件 [英] Get File from external URL with fs.readFile

查看:820
本文介绍了使用fs.readFile从外部URL获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有链接,当我单击该链接时,我希望打开一个外部docx文件.不幸的是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.

我尝试了

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

这篇关于使用fs.readFile从外部URL获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆