Your workflow is unique 👨💻 - tell us how you use Dropbox here.
Forum Discussion
Madhavi T.
9 years agoExplorer | Level 4
Dropbox API v2
I am trying out Dropbox API V2 using HTMl,JavaScript by making API calls with XMLHTTPRequest. To upload a file onto Dropbox the API requires a 'data-binary' property to be set with the path of the file you want to upload. Below is the reference link of API and its parameters:
https://www.dropbox.com/developers/documentation/http/documentation#files-upload
I am able to set all the other properties and API is able to create a blank file at the specified path for 'Dropbox-API-Arg', however unable to pass the file from my machine to the API call.
How can I set data-binary with the path of the file to upload, specifically in XMLHTTPRequest?
Thanks in advance.
4 Replies
- Greg-DB9 years ago
Dropbox Community Moderator
The HTTP documentation has a curl example, where curl's "data-binary" option is used to supply the file request body from a local file.
In JavaScript in the browser, you don't have direct access to use the user's local filesystem, so when using XMLHttpRequest, you can supply the file data directly by using the "send" method. There's an example here:
https://stackoverflow.com/documentation/dropbox-api/409/uploading-a-file/18435/uploading-a-file-from-text-via-xmlhttprequest-in-javascript#t=201702021826219362095 - Madhavi T.9 years agoExplorer | Level 4
Thanks for the useful information Greg. ☺
I tried out the way you suggested and it seems that if I write a string (any sentence) in the value of the variable 'content' and pass it to the request keeping the path as a text file, it indeed creates a text file with the contents in it. However, I would like to pass the contents of an PDF file/image file to the request. When I try to get a file thorugh HTML input and pass it to the request, it simply creates a 0 bytes file in Dropbox with no content in it, and says the file can not be previewed. I believe this is happening because PDF files contains binary data and I am unable to do a binary-string conversion using JavaScript.
Could you please let me know if this is something you can help with. Any help is greatly appreciated. Thanks in advance.
- Greg-DB9 years ago
Dropbox Community Moderator
There's another example for uploading from a file from an input tag here:
https://stackoverflow.com/documentation/dropbox-api/409/uploading-a-file/1359/uploading-a-file-via-jquery-in-javascript#t=201702061903317042582 - Madhavi T.9 years agoExplorer | Level 4
Awesome...Thanks a ton Greg!!! I just tried it and it seems to be working for me... ☺
This is a great forum. I have found a good platform in case I am stuck on anything!!!
About Dropbox API Support and Feedback
Get help with the Dropbox API from fellow developers and experts.
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!