用http下载文件

时间:2014-09-12 18:57:31

标签: node.js http

我尝试使用以下代码从链接下载pdf文件。但是,我得到了404错误页面。相同的链接在浏览器中工作正常。原因是什么?

var http = require('http');
var options = {
  host: 'www.xxxx.xxx',
  path: '/achieved/a1.pdf'
};

callback = function(response) {
  var str = '';
 //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
  str += chunk;
});

//the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });
}

http.request(options, callback).end();

0 个答案:

没有答案