我想从node.js中的url下载文件吗?

时间:2018-10-28 05:18:49

标签: node.js http http-status-code-403

const BC = require('nse-bhavcopy');
const options = {
 dir: "./",
 headers : {
   'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0'
 }
};
const request = new BC(options);
request
  .download({
    month : "JAN",
    year : 2008
  })
  .then(data=>console.log(data))
  .catch(err=>console.log(err));

我想下载整个月的报告,但无法下载。但是,当我转到浏览器并尝试下载时,它可以完美运行。

因此,我在互联网上阅读了有关他们还提到用户代理的其他答案,但他们做不到。它显示消息为访问被拒绝。请帮忙解决这个问题?并且显示状态码为403。

任何想法都值得赞赏!

我得到这样的东西:

[ { message: 'Access Denied: for the file on date 01JAN2008' },
  { message: 'Access Denied: for the file on date 02JAN2008' },
  { message: 'Access Denied: for the file on date 03JAN2008' },
  { message: 'Access Denied: for the file on date 04JAN2008' },
  { message: 'Access Denied: for the file on date 05JAN2008' },
  { message: 'Access Denied: for the file on date 06JAN2008' },
  { message: 'Access Denied: for the file on date 07JAN2008' },
  { message: 'Access Denied: for the file on date 08JAN2008' },
  { message: 'Access Denied: for the file on date 09JAN2008' },
  { message: 'Access Denied: for the file on date 10JAN2008' },
  { message: 'Access Denied: for the file on date 11JAN2008' },
  { message: 'Access Denied: for the file on date 12JAN2008' },
  { message: 'Access Denied: for the file on date 13JAN2008' },
  { message: 'Access Denied: for the file on date 14JAN2008' },
  { message: 'Access Denied: for the file on date 15JAN2008' },
  { message: 'Access Denied: for the file on date 16JAN2008' },
  { message: 'Access Denied: for the file on date 17JAN2008' },
  { message: 'Access Denied: for the file on date 18JAN2008' },
  { message: 'Access Denied: for the file on date 19JAN2008' },
  { message: 'Access Denied: for the file on date 20JAN2008' },
  { message: 'Access Denied: for the file on date 21JAN2008' },
  { message: 'Access Denied: for the file on date 22JAN2008' },
  { message: 'Access Denied: for the file on date 23JAN2008' },
  { message: 'Access Denied: for the file on date 24JAN2008' },
  { message: 'Access Denied: for the file on date 25JAN2008' },
  { message: 'Access Denied: for the file on date 26JAN2008' },
  { message: 'Access Denied: for the file on date 27JAN2008' },
  { message: 'Access Denied: for the file on date 28JAN2008' },
  { message: 'Access Denied: for the file on date 29JAN2008' },
  { message: 'Access Denied: for the file on date 30JAN2008' },
  { message: 'Access Denied: for the file on date 31JAN2008' } ]

1 个答案:

答案 0 :(得分:0)

将您的目录位置更改为path.join(__dirname,'')

尝试一下。

const BC = require('nse-bhavcopy');
const path = require('path');
const options = {
 dir: path.join(__dirname,'')
};
const request = new BC(options);
request
  .download({
    month : "JAN",
    year : 2008
  })
  .then(data=>console.log(data))
  .catch(err=>console.log(err));