2016-07-09 27 views
2

我在頁面上點擊鏈接時想要打開外部docx文件。不幸的是fs.readFile只能讀取本地路徑。使用fs.readFile從外部URL獲取文件

我試圖

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

不要使用您已發佈 –

+0

SCRIPT7002代碼時,你得到什麼錯誤/輸出網絡錯誤0x2efd,無法完成該操作由於錯誤00002efd。 –

+0

是有用的嗎?標記爲解決該問題。 –

回答

10

試試這個:XMLHttpRequest的:

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

http.get("http://www.example.com/test.docx", response => { 
    response.pipe(file); 
});