node-dropbox
node-dropbox copied to clipboard
write file to disk
Hey! Thanks for that great module. I am having trouble to download a file using the getFile method. It just creates an empty file.
here is my code:
api.getFile('/personal/1.jpg', function(err, res, body) {
var file = fs.createWriteStream("1.jpg");
res.pipe(file);
});
Hmm I'll take a look when I have some time this week.
Great. getMetadata works fine getFile is giving me trouble.
`var api = node_dropbox.api(ACCESS_TOKEN);
// works fine
api.getMetadata('/personal', function(err, res, body) {
console.log(err, body);
});
api.getFile('/personal/1.jpg', function(err, res, body) {
var file = fs.createWriteStream("1.jpg");
// writes an empty file to disk
res.pipe(file);
});`
If anyone is looking at this or if you still need help with this, this is because you are pipeing the response, but you should be piping the request object, which is not returned.
To fix this, this line needs to be updated to
getFile: function(path, cb) {
options = optionsBuilder(apiContentRoot + api.getFiles + '/auto' + path, access_token);
return request(options, cb)
},
so that you can run
api.getFile('/personal/1.jpg').pipe(fs.createWriteStream('1.jpg');
@ChrisAlvares is there no way to make pipe without your code? @g33kidd can you add it to the lib? If you like i can make PR.
@CrackerakiUA
I would highly suggest making a PR. In the meantime, you can also do it manually using the new v2 dropbox api. Your Api token will still work.
var request = require('request');
var fs = require('fs');
function downloadFile(accessToken, dropboxFile, filePath, callback) {
request({
uri: 'https://content.dropboxapi.com/2/files/download',
headers: {
Authorization: 'Bearer ' + accessToken,
'Dropbox-API-Arg': JSON.stringify({path: dropboxFile})
}
})
.on('error', callback)
.on('end', (error) => {
callback(error, filepath);
})
.pipe(fs.createWriteStream(filePath));
}
@ChrisAlvares temporary i have made fork. Waiting for answer from @g33kidd
@CrackerakiUA Did not see your previous ping, whoops... Yes, that would be awesome if you could make a PR. Currently I'm working on the next version of this package since this is quite outdated and there's a new API version!
@g33kidd i will also make small tutor under wiki.
Noice! Thanks for doing that.
@g33kidd please publish to npm
@CrackerakiUA Done! Updated to 0.1.8
as of now.
@g33kidd Thanks