Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
amei
4 months agoExplorer | Level 3
Using javascript to download file from dropbox
I am creating a javascript routine that requires me to download and upload updated files in my dropbox app file folders which contains updated football picks. I am able to upload files to my app but when I try to upload I get an undefined error.
file path :
download functions (I tried both of these) :
Both of these functions return the same error :
Any assistance would be greatly appreciated. Thanks.
14 Replies
- amei4 months agoExplorer | Level 3
LoadFileFromDropbox error (other load function attempt)
- DB-Des4 months ago
Dropbox Community Moderator
Hi amei
The response from 'filesDownload()' does not contain a 'fileBlob' property (which is why you are getting an 'undefined' in the error message). The response contains a 'fileBinary', which can be accessed as such: 'response.result.fileBinary'. Additionally, you can convert the returned Buffer to text or a string by invoking the 'toString()' method on the response. We would also recommend making that callback function an async function:
... dbx.filesDownload({ path: dropbox_users_path_file }) .then(async (response) => { console.log(response.result.fileBinary.toString()) } ...Similarly for the second function:
... try { const response = await dbx.filesDownload({ path: dropbox_users_path_file }) console.log(response.result.fileBinary.toString()) } ...In order to further assist with the file upload error, please provide the code snippet used to upload files to Dropbox. The screenshots provided are both for downloading files ('filesDownload()').
- amei4 months agoExplorer | Level 3
thanks for your assistance. I updated both functions as you suggested :
Still getting the same errors :
The jason file that I have uploaded from my upload function looks like this :
It is an array of football picks. So when I upload the files I used the JSON.stringify to save the array. When I open the jason file I see text, not binary characters. Not sure if this is the problem. Perhaps a text response would ? I need to download the file back into the array (what the file is currently saved as as shown above.
I should have included this info in my initial post. I hope this provides more clarity. thanks.
- amei4 months agoExplorer | Level 3
I updated my download function as :
I get a successful download, but I don't see the one line of football picks, just a blank array. The array is formatted correctly, but does not contain any information.
I changed fileBinary to fileText. I'm not sure if I have to define that it is a json file.
- DB-Des4 months ago
Dropbox Community Moderator
Could you share the actual error that is being logged onto the console? Both the top-level 'error' and the lower-level 'error.error'.
- amei4 months agoExplorer | Level 3
Hi DB-DEs
My request is at the bottom with how to donwload a jason text file from my dropbox app to an array called users_array. Here is somebackground before the request at the bottom.
With your assistance I was able to upload a stringified Jason file to my dropbox ap. Download function below :
The uploaded jason file is a text file (not binary) and is an array of football picks as follows :
Here are the functions I tried to use to upload the file above into an array called users_array. As you can see I tried 3 different methods. The LoadFileFromDropbox function gives me a promise with the array content, just can't get the content.
- amei4 months agoExplorer | Level 3
the LoadFromDropbox returns the following :
The LoadFromDropbox2 returns essentially the same
- iNeil4 months ago
Dropbox Community Moderator
Hi amei,
I have reviewed the information you provided. From my understanding, you are experiencing difficulties downloading a file using the Dropbox JavaScript SDK and parsing a JSON file.
Based on the code snippet you shared, it appears that the JSON file is being parsed incorrectly. Would you be able to test the following code snippet example to check whether it resolves the issue you are encountering?import { Dropbox } from "dropbox"; import fs from "fs/promises"; const dbx = new Dropbox({ accessToken: "YOUR_ACCESS_TOKEN" }); async function downloadJsonFile() { try { // Request file from Dropbox const { result } = await dbx.filesDownload({ path: "/users_array.json" }); // Convert response into a buffer let buffer; if (result.fileBinary) { buffer = Buffer.from(result.fileBinary); } else if (result.fileBlob) { const ab = await result.fileBlob.arrayBuffer(); buffer = Buffer.from(ab); } else { throw new Error("Unexpected response: no fileBinary/fileBlob present."); } // Download the file to a specified location on your local machine. await fs.writeFile("/YOUR_FILE_PATH_TO/users_array.json", buffer); console.log("File downloaded successfully"); // Parsing JSON file after downloading the file const parsed = JSON.parse(buffer.toString("utf8")); console.log("Parsed JSON object:", parsed); } catch (error) { console.error(" Error downloading file:", error); } } downloadJsonFile(); - amei4 months agoExplorer | Level 3
Thanks for pointing me in the right direction. I finally figured out that I was working with a string(text) representation of an array uploaded to dropbox as a json file. Once I realized that this is what I did to properly download the jason file and etablish the users_array containing football picks :
Array Listing : (note I only picked one line)
When I tried to call up a particular value in the array to make sure it's working :
So the array has been downloaded and formatted properly, Thanks for pointing me in the right direction.
The only issue I have now is that I have been working with a temporary access code (starts with sl and lasts only 4 hours). Is there an option to make the code last for a week or more ?
- iNeil4 months ago
Dropbox Community Moderator
Hi amei, I'm happy to hear that you are able to resolve the issue with downloading the JSON file with the Dropbox API!
In relation to the access token, it's not possible to extend the access token's expiry time. Please bear in mind that Dropbox is no longer offering the option for creating new long-lived access tokens. Dropbox is now issuing short-lived access tokens (and optional refresh tokens) instead of long-lived access tokens. You can find more information on this migration here.
Apps can still get long-term access by requesting "offline" access though, in which case the app receives a "refresh token" that can be used to retrieve new short-lived access tokens as needed, without further manual user intervention. Refresh tokens do not expire automatically and can be used repeatedly. You can find more information in the OAuth Guide and authorization documentation. There's a basic outline of processing this flow in this blog post which may serve as a useful example.
About Discuss Dropbox Developer & API
Make connections with other developers
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!