Reputation: 5194
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?
Upvotes: 1
Views: 4247
Reputation: 1070
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}`);
}
}
Upvotes: 0
Reputation: 3434
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).
Upvotes: 3