UNPKG

1.61 kBMarkdownView Raw
1# download [![Build Status](https://travis-ci.org/kevva/download.svg?branch=master)](https://travis-ci.org/kevva/download)
2
3> Download and extract files
4
5*See [download-cli](https://github.com/kevva/download-cli) for the command-line version.*
6
7
8## Install
9
10```
11$ npm install download
12```
13
14
15## Usage
16
17```js
18const fs = require('fs');
19const download = require('download');
20
21(async () => {
22 await download('http://unicorn.com/foo.jpg', 'dist');
23
24 fs.writeFileSync('dist/foo.jpg', await download('http://unicorn.com/foo.jpg'));
25
26 download('unicorn.com/foo.jpg').pipe(fs.createWriteStream('dist/foo.jpg'));
27
28 await Promise.all([
29 'unicorn.com/foo.jpg',
30 'cats.com/dancing.gif'
31 ].map(url => download(url, 'dist')));
32})();
33```
34
35### Proxies
36
37To work with proxies, read the [`got documentation`](https://github.com/sindresorhus/got#proxies).
38
39
40## API
41
42### download(url, destination?, options?)
43
44Returns both a `Promise<Buffer>` and a [Duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with [additional events](https://github.com/sindresorhus/got#streams-1).
45
46#### url
47
48Type: `string`
49
50URL to download.
51
52#### destination
53
54Type: `string`
55
56Path to where your file will be written.
57
58#### options
59
60Type: `Object`
61
62Same options as [`got`](https://github.com/sindresorhus/got#options) and [`decompress`](https://github.com/kevva/decompress#options) in addition to the ones below.
63
64##### extract
65
66Type: `boolean`<br>
67Default: `false`
68
69If set to `true`, try extracting the file using [`decompress`](https://github.com/kevva/decompress).
70
71##### filename
72
73Type: `string`
74
75Name of the saved file.