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.
Any idea what I could be doing wrong? Am I making a mistake in the code above?
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).