The issue:
I need to download a PDF file from my server but getting either "No file" or empty file
Details:
Here is my server-side code:
let fileBuffered = ''; // authentication for downloading a file from Dropbox API to my server const dropbox = dropboxV2Api.authenticate({ token: process.env.DEV_DROPBOX_SECRET_KEY }); // configuring parameters const params = Object.freeze({ resource: "files/download", parameters: { path: `/${customerFileFolder}/${fileName}` } }); let dropboxPromise = new Promise(function (resolve, reject) { dropbox(params, function (err, result) { if (err) { reject(err); } else { resolve(result); } }).on('data',function(data) { fileBuffered += data; }) const file = fileBuffered; res.setHeader("Content-Type", "application/octet-stream"); res.send(file); The PDF file's that I'm trying to download size is 139,694 bytes. The length of the fileBuffered variable is 132,597. Here is the content of the variable as it is shown in the debugger:
Seems like a legit PDF file
Here is the client-side
function documentFileDownload(fileName) { const ip = location.host; let request = $.ajax({ type: "POST", url: `${http() + ip}/documentFileDownload`, headers: { "Accept": "application/octet-stream" }, data: { fileName: fileName }, error: function (err) { console.log("ERROR: " + err); } }); console.log(request); return request; } Problem: Then I get the response on a client-side it looks like this:
Note the size of the responseText: 254Kb. What I actually get in the browser is a "Failed - No file" message
What else I tried:
I tried to play with different Content-Types (application/pdf, text/pdf) on a server-side and tried to convert the variable to base64 buffer
const file = `data:application/pdf;base64, ${Buffer.from(fileBuffered).toString("base64")}`; and added res.setHeader("Accept-Encoding", "base64"); but still getting the same result.
Any ideas?

