node-dropbox icon indicating copy to clipboard operation
node-dropbox copied to clipboard

write file to disk

Open adai183 opened this issue 8 years ago • 12 comments

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);

});

adai183 avatar Jun 24 '16 11:06 adai183

Hmm I'll take a look when I have some time this week.

g33kidd avatar Jun 27 '16 07:06 g33kidd

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);
});`

adai183 avatar Jun 27 '16 11:06 adai183

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 avatar Aug 24 '16 15:08 ChrisAlvares

@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 avatar Aug 26 '16 16:08 CrackerakiUA

@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 avatar Aug 28 '16 15:08 ChrisAlvares

@ChrisAlvares temporary i have made fork. Waiting for answer from @g33kidd

CrackerakiUA avatar Aug 29 '16 13:08 CrackerakiUA

@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 avatar Aug 29 '16 14:08 g33kidd

@g33kidd i will also make small tutor under wiki.

CrackerakiUA avatar Aug 30 '16 10:08 CrackerakiUA

Noice! Thanks for doing that.

g33kidd avatar Aug 30 '16 22:08 g33kidd

@g33kidd please publish to npm

CrackerakiUA avatar Aug 31 '16 13:08 CrackerakiUA

@CrackerakiUA Done! Updated to 0.1.8 as of now.

g33kidd avatar Aug 31 '16 14:08 g33kidd

@g33kidd Thanks

CrackerakiUA avatar Aug 31 '16 14:08 CrackerakiUA