How To Download A Big File From Dropbox With Node.js?
I want to implement a big file downloading (approx. 10-1024 Mb). I've already succeeded to get a file from Dropbox: operationResult = await dbx.filesDownload({ path: `/${CONFIG
Solution 1:
I've solved the issue by switching from filesDownload
to filesGetTemporaryLink
, which returns a link to a file instead of the file itself. Then I trigger a downloading of this link.
The final result:
operationResult = await dbx.filesGetTemporaryLink({
path: `/${CONFIG_STORAGE.uploader.assetsPath}/${fileUUID}`
});
const downloadResult = Object.freeze({
fileLength: operationResult?.metadata.size,
fileLink: operationResult?.link,
fileMIME: mime.lookup(operationResult?.metadata.name),
fileName: operationResult?.metadata.name,
isSucceeded,
message
});
return downloadResult;
Then I send the output to a client:
res.json(downloadResult);
On a client-side I get it via await
/async
Fetch API call:
const fileResponse = await fetch(``${host}/downloadDocument`, {
body: JSON.stringify({fileUUID: fileName}),
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json"
},
method: "POST",
mode: "cors"
});
const fileData = await fileResponse.json();
const aTag = document.createElement("a");
aTag.href = fileData.fileLink;
aTag.download = fileData.fileName;
aTag.click();
As a result, a server doesn't have to deal with files at all, no extra CPU, RAM, or traffic impact, no matter what size of a file I'm trying to download.
Post a Comment for "How To Download A Big File From Dropbox With Node.js?"