Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
docs(README): fix the file download example
Browse files Browse the repository at this point in the history
docs(README): add comment to README
  • Loading branch information
Chih Hung Chen authored and Chih Hung Chen committed Dec 14, 2017
1 parent dc1dd62 commit a23a60a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ async.series([
});
},

function(cb){
function (cb){
// and now we're going to download that file we just uploaded
// method 1 : use Buffer to concatenate each chunks
kloudless.files.contents({
"account_id": accountId,
"file_id": fileId
Expand All @@ -82,17 +83,34 @@ async.series([
}
var filecontents = '';
console.log("got the filestream:");
filestream.on('data', function(chunk) {
filestream.on('data', function (chunk) {
console.log("reading in data chunk...");
console.log(chunk);
filecontents += chunk;
filecontents = Buffer.concat([filecontents, chunk]);
});
filestream.on('end',function() {
filestream.on('end', function () {
console.log("finished reading file!");
console.log(filecontents);
fs.writeFile("download.jpg", filecontents, function (err) {
console.log('write file error:' + err);
});
cb();
});
});
},
function (cb) {
// and now we're going to download that file we just uploaded
// method 2 : pipe the filestream directly
kloudless.files.contents({
"account_id": accountId,
"file_id": fileId
}, function (err, filestream) {
if (err) {
return console.log("Files contents: " + err);
}
console.log("got the filestream:");
filestream.pipe(fs.createWriteStream('download_2.jpg'));
cb();
});
}
]);
```
Expand Down

0 comments on commit a23a60a

Please sign in to comment.