介绍:
download可以下载和提取文件, download-cli是它的命令行版本。
安装:
$ npm install --save download
用法:
如果抓取一个压缩包的话,你可以在选项中设置extract: true
,它就会为你解压缩。
var Download = require('download');
new Download({mode: '755'})
.get('http://example.com/foo.zip')
.get('http://example.com/cat.jpg')
.dest('dest')
.run();
API讲解:
1.new Download(options)
:
创建一个Download
实例,其中options
参数的类型是对象。
options
参数可以为:
extract
:类型是boolean
,默认值是false
。如果设置为true
的话,会尝试使用decompress来接压缩;mode
:类型是string
,为下载的文件设置权限。例如:{mode: '755'}
;strip
:类型是number
,默认值是0
,从解压缩的文件中移除前面的目录组件;
2..get(url, [dest])
:
参数讲解:
url
:类型是string
,添加一个下载的URL;dest
:类型是string
,设置一个可选的目标目录,它是优先于在.dest()
设置的目录;
3..dest(dir)
:
参数讲解:
dir
:类型是string
,设置目标目录来存放所下载的文件;
4..rename(name)
:
参数讲解:
name
:类型是function
或者string
,使用gulp-rename来重命名你的文件。
5..use(plugin)
:
参数讲解:
plugin的类型是function
,给中间件栈上添加一个插件:plugin(response, url)
,其中:
response
:是response
对象;url
:是请求的URL。
6..run(callback)
:
参数讲解:
callback的类型是function
:callback(err, files)
,其中:
files
:包含文件的数组。