Skip to content
This repository was archived by the owner on Feb 5, 2022. It is now read-only.

Commit 032a618

Browse files
committed
Adds example of downloading file from server with progress bar in README.md
1 parent 589a659 commit 032a618

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,42 @@ The high level client is an instance of `Client`, but it contains the high level
191191

192192
download a server file to local.
193193

194+
### Example with progress bar (download from server)
195+
196+
You can easily combine this module with the `progress` npm package, to get live progress data in your commandline interface. Example below:
197+
198+
```javascript
199+
var scp2 = require('scp2');
200+
var ProgressBar = require('progress');
201+
202+
var client = new scp2.Client();
203+
var bar;
204+
205+
client.on('transfer', function(buf, downloaded, total) {
206+
if (!bar) {
207+
bar = new ProgressBar(' downloading [:bar] :rate/bps :percent :etas', {
208+
complete: '=',
209+
incomplete: ' ',
210+
width: 20,
211+
total: total
212+
});
213+
}
214+
bar.tick(buf.length);
215+
});
216+
217+
scp2.scp({
218+
host: ...,
219+
username: ...,
220+
password: ...,
221+
path: '/path/to/src/file'
222+
}, '/path/to/dest/file', client, function(err) {
223+
if (err) {
224+
console.log(err);
225+
}
226+
227+
// Do stuff with the downloaded file
228+
});
229+
```
194230

195231
## Events
196232

lib/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ Client.prototype.download = function(src, dest, callback) {
295295
sftp.open(src, 'r', function(err, fd) {
296296
sftp.fstat(fd, function(err, stats) {
297297
var bufferSize = stats.size;
298+
var bytesDownloaded = 0;
298299

299300
var sftp_readStream = sftp.createReadStream(src);
300301
sftp_readStream.on('data', function(data) {

0 commit comments

Comments
 (0)