Excel files uploaded to Dropbox using Node.js SDK are invalid

169 views Asked by At

I am trying to upload Excel files to Dropbox using the Node.js SDK (https://github.com/dropbox/dropbox-sdk-js). The files are received from JsReport, and are saved correctly when saved to my local disk. However, when uploading to Dropbox, the files end up long strings instead of Excel files.

The body in this case is a string that gets encoded to base64. The filename is speedtest_daily_2021-01-15_test26.xlsx.

This line writes the file successfully to my local drive:

fs.writeFileSync(*filename*, body, {encoding: 'base64'});

This is the code I'm using to upload the file to Dropbox:

body = Buffer.from(body).toString('base64');

dbx.filesUpload({ contents: body, path: saveLocation + `/` + filename })
          .then((response: any) => {
            console.log(`Saved file "${filename}" to Dropbox folder ${saveLocation}`);
          }).catch((uploadErr: DropboxError<files.UploadError>) => {
          console.log(uploadErr);
        });

This is how the Excel file should look, and looks on my local:

enter image description here

This is what happens when I open the Dropbox Excel file:

enter image description here

This is how the Dropbox file looks when I open it up in a text editor:

enter image description here

I've tried various encoding combinations for Dropbox but none of them seem to work.

1

There are 1 answers

0
Nathanael Troyer On

Figured it out. What I had to do was send the buffer to filesUpload, not the encoded string.

let buffer = Buffer.from(body, 'base64');
    dbx.filesUpload({ contents: buffer, path: saveLocation + `/` + filename })
      .then((response: any) => {
        console.log(`Saved file "${filename}" to Dropbox folder ${saveLocation}`);
      }).catch((uploadErr: DropboxError<files.UploadError>) => {
      console.log(uploadErr);
    });