nodejs如何发送请求excel文件并下载

xlsx文件地址为:http://www.baidu.com/down?id=1
用nodejs如何发送http请求下载这个excel文件!
我用的是express框架,用request发送请求

var request = require('request');
router.get('/test', function (req, res) {
    req.pipe(request('https://www.baidu.com/img/bd_logo1.png')).pipe(res);
})

比如图换成图片,则可以显示,但excel文件的话就不能下载了!

阅读 4.7k
1 个回答

res.download(path [, filename] [, fn]) http://expressjs.com/en/api.h...

Transfers the file at path as an “attachment”. Typically, browsers will prompt the user for download. By default, the Content-Disposition header “filename=” parameter is path (this typically appears in the browser dialog). Override this default with the filename parameter.

When an error ocurrs or transfer is complete, the method calls the optional callback function fn. This method uses res.sendFile() to transfer the file.

res.download('/report-12345.pdf');

res.download('/report-12345.pdf', 'report.pdf');

res.download('/report-12345.pdf', 'report.pdf', function(err){
  if (err) {
    // Handle error, but keep in mind the response may be partially-sent
    // so check res.headersSent
  } else {
    // decrement a download credit, etc.
  }
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题