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
|
18 | const fs = require('fs');
|
19 | const 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 |
|
37 | To work with proxies, read the [`got documentation`](https://github.com/sindresorhus/got#proxies).
|
38 |
|
39 |
|
40 | ## API
|
41 |
|
42 | ### download(url, destination?, options?)
|
43 |
|
44 | Returns 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 |
|
48 | Type: `string`
|
49 |
|
50 | URL to download.
|
51 |
|
52 | #### destination
|
53 |
|
54 | Type: `string`
|
55 |
|
56 | Path to where your file will be written.
|
57 |
|
58 | #### options
|
59 |
|
60 | Type: `Object`
|
61 |
|
62 | Same 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 |
|
66 | Type: `boolean`<br>
|
67 | Default: `false`
|
68 |
|
69 | If set to `true`, try extracting the file using [`decompress`](https://github.com/kevva/decompress).
|
70 |
|
71 | ##### filename
|
72 |
|
73 | Type: `string`
|
74 |
|
75 | Name of the saved file.
|