1

I am trying to upload a file from Node.js to my dropbox account. I have created an app on dropbox developer console and then generated a Access Token.

I am using the following code to read and upload a file to dropbox:

app.get('/uploadfile', function (req, res) {

  if (req.query.error) {
    return res.send('ERROR ' + req.query.error + ': ' + req.query.error_description);
  }

  fs.readFile(__dirname + '/files/pictureicon.png','utf8', function read(err, data) {
    if (err) {
      throw err;
    }
    fileupload(data);
  });
});

function fileupload(content) {
  request.put('https://api-content.dropbox.com/1/files_put/auto/proposals/icon.png', {
    headers: { Authorization: 'Bearer TOKEN-HERE', 'Content-Type': 'image/png'
  }, body: content}, function optionalCallback (err, httpResponse, bodymsg) {
    if (err) {
      return console.log(err);
    } 

    console.log("HERE");
  });
}

By using the above code my file appears in my dropbox account but I am unable to open it. It comes up with the following error.

enter image description here

Any idea what I could be doing wrong? Am I making a mistake in the code above?

2 Answers 2

3

The problem is probably that you read the file with the encoding utf-8, even though it is not a text file. You should read the buffer (by simply not providing a encoding argument).

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

1 Comment

Yup that was problem. Thanks for the help!!
0

I know it is a bit late hope this will help someone,

const axios = require('axios');
const fs = require('fs');

  const uploadFile = async () => {
        try {
          const response = await axios({
            method: `POST`,
            url: `https://content.dropboxapi.com/2/files/upload`,
            headers: {
              Authorization: `Bearer ${AUTH_TOKEN}`,
              'Content-Type': 'application/octet-stream',
              'Dropbox-API-Arg': '{"path":"/testfolder/isp.png"}',//file path of dropbox
            },
            data: fs.createReadStream(__dirname + '/isp.png'),//local path to uploading file
          });
          console.log(response.data);
        } catch (err) {
          return console.log(`X ${err.message}`);
        }
      }

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.