1

ORIGINAL

I'm having problems to upload a file (image) to Dropbox from Node.js using the official dropbox.js. I want to upload a picture that I have in another server. For example with the dropbpox icon (www.dropbox.com/static/images/new_logo.png).

client.writeFile(file, 'www.dropbox.com/static/images/new_logo.png', function(error, stat) {
 if (error) {
  return es.send(error.status);  // Something went wrong.
 }

 res.send("File saved as revision " + stat.revisionTag);
});

I know that this only creates a text file with the url, but how I can upload the picture to Dropbox? I also try to download the file using http.get and then upload this to dropbox but it doesn't work.

Thanks.

UPDATE WITH MORE INFO

First I download the image from a remote url with this code:

var request = http.get(options, function(res){
        var imagedata = ''
        res.setEncoding('binary')

        res.on('data', function(chunk){
            imagedata += chunk
        }) 

        res.on('end', function(){ 
            console.log("Image downloaded!");

            fs.writeFile(local, imagedata, 'binary', function(err){
              if (err) throw err
              console.log('File saved.')
            })
        })
    })

The file is saved correctly. Then I trie to things:

Sending the 'imagedata' to Dropbox:

  console.log("Image downloaded!");

            client.writeFile(file, imagedata, function(error, stat) {
                if (error) {
                    return response.send(error.status);  // Something went wrong.
                }

                response.send("File saved as revision " + stat.revisionTag);
            });

And something is uploaded to Dropbox but it's nothing useful.

Then I also tried to read the file from disc and then send it to Dropbox but it doesn't work neither:

fs.readFile(file, function(err, data) {
1
  • Please don't hesitate to open issues on the dropbox-js GitHub page! Commented Nov 26, 2012 at 3:31

2 Answers 2

3

Use dropbox-js 0.9.1-beta1 or above to upload binary files from node.js. You need to pass it Buffer or ArrayBuffer instances. Try this code:

var req = http.get(options, function(res) {
  var chunks = [];

  res.on('data', function(chunk) {
    chunks.push(chunk);
  });

  res.on('end', function() {
    console.log("Image downloaded!");

    var imageData = Buffer.concat(chunks);
    client.writeFile(file, imageData, function(error, stat) {
      if (error) {
        return response.send(error.status);
      }
      response.send("File saved as revision " + stat.revisionTag);
    });
  });
});

```

Original answer: the dropbox-js README mentions that binary files don't work in node.js just yet.

Sign up to request clarification or add additional context in comments.

2 Comments

I've updated the post with what I do. I do something wrong or it's the library? Thanks! ;)
@alejandromp: It's the library. Sorry, but I haven't had bandwidth to fix binary writes in node.js.
0

I had issue as well, I just copied and modified a bit on the old dropbox-node npm(which is now deprecated), but I added following function on dropbox.js.

Client.prototype.writeFileNodejs = function(path, data, callback) {
  var self = this;
  fs.readFile(data.path, function(err, data) {
    if (err) return callback(err);
    var uri = "" + self.urls.putFile + "/" + (self.urlEncodePath(path));

    if (typeof data === 'function') callback = data, data = undefined;
    var oauth = {
      consumer_key: self.oauth.key
    , consumer_secret: self.oauth.secret
    , token: self.oauth.token
    , token_secret: self.oauth.tokenSecret
    };

    var requestOptions = { uri: uri, oauth: oauth };
    requestOptions.body = data;

    return request['put'](requestOptions, callback ?
      function(err, res, body) {
        if (err) return callback(err);
        var contentType = res.headers['content-type'];

        // check if the response body is in JSON format
        if (contentType === 'application/json' ||
            contentType === 'text/javascript') {
          body = JSON.parse(body);
          if (body.error) {
            var err = new Error(body.error);
            err.statusCode = res.statusCode;
            return callback(err);
          }

        } else if (errors[res.statusCode]) {
          var err = new Error(errors[res.statusCode]);
          err.statusCode = res.statusCode;
          return callback(err);
        }

        // check for metadata in headers
        if (res.headers['x-dropbox-metadata']) {
          var metadata = JSON.parse(res.headers['x-dropbox-metadata']);
        }

        callback(null, body, metadata);
      } : undefined);
  });
};

As well you would like to require request and fs to do this.

var request = require('request'),
    fs = require('fs');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.