dropbox-sdk-js
dropbox-sdk-js copied to clipboard
Add complex examples
We will want to include more complex examples, as well as examples that show how to obtain an access token.
+1, more examples are very important for developers getting started with a new library. E.g., cover different methods, non-default parameters, etc.
@greg-db do you think this is still needed? any ideas what a good example would be that isn't covered by our existing examples?
@RileyTomasek Looking through, a few more things that would be useful, in some rough order of importance in my opinion:
- catching specific errors (e.g., how do I programmatically differentiate between
path.not_found
andpath.not_file
from /download) but that may be related to #50. - using
filesListFolderContinue
along withfilesListFolder
. - using upload sessions.
- sharing a folder.
I set myself a simple task of listing the files in a folder. I have iterated through them using filesListFolder
, but couldn't see an example of how to link to these. (There is a "download" example, but I for that you need a share-link).
@amk221 You don't need to first get a link to download a file. (The download example just happens to do so.)
You can download a file without a shared link by supplying the file path to the filesDownload
method.
If you do want a shared link though, you would use sharingCreateSharedLinkWithSettings
.
Right, totally missed that! Cheers. So that lets me download a file, but not build a hyperlink to that file though...
This definitely needs to happen I think. Anyone new coming to the library is completely baffled by the lack of documentation and examples. If there's no documentation then there should be a healthy number of examples to make up for it. Or vice versa. With neither, you're left guessing and pulling straws and hoping something works. Even a few more complex examples like sharing folders, setting access levels, etc, would really help.
Thanks for the additional feedback @elburz !
I would appreciate an example of
- how to show the progress of an upload
- how to do an in-browser view of a file
Thanks for the feedback @kennellink !
I would like to see examples how to manage access_tokens in web applications.
I would like to see an approach which is as transparent as possible for the user while keep it as simple as possible. So what I would do is
- App has a login button: it does the authentification and stores the access_token in local store
- subsequent visits on the app use this access token
- user can decide to logout. This would revoke the access token and also delete the entry in local store
I tried this and would appreciate your comment.
see also discussion in #48, #49, #67
Definitely missing an exmple equvalent to
client.readFile("hello_world.txt", function(error, data) {
if (error) {
return showError(error); // Something went wrong.
}
alert(data); // data has the file's contents
});
I think using most of the basic functionalities is relatively simple now - it just requires looking for needed methods in HTTP API, and finding its equivalents in /src of this JS SDK. Parameters and responses are the same, even method names are predictable.
Following this approach, I wrote something yesterday - https://github.com/ppietak/electron-dropbox-screenshot It involves simple authorization, file upload and obtaining shared link as well.
@ppietak In V1 it was very simple. With V2 I find it rather not simple ...
Just found this solution
%x{#@root.filesDownload({path: #{filename}})
.then(function(response){
reader = new FileReader();
reader.addEventListener("loadend", function(){
alert(reader.result);
});
reader.readAsText(response.fileBlob);
})
.catch(function(error){#{iblock}(error, nil)})
}
I think stuff like this could be builtin to the javascript sdk, maybe even with the same name as in V1.
Thanks for the feedback all!
What @bwl21 said.
I've spent hours looking around for something as simple as how to read the contents of a Dropbox file, before I came across his code. It would be certainly be nice to have that be included as part of the SDK. At the very least, it should be part of the documentation and/or examples.
Since I'm trying to use this in a cordova application, I'd love to see JavaScript code for accessing an App Folder and not just regular dropbox files
@tommck I'm not sure I follow. Can you elaborate? Which Dropbox API permission (e.g., "app folder" or "Full Dropbox") you app has doesn't affect how paths are supplied. For example, a path like "/Documents/resume.docx" identifies a file "resume.docx" inside a "Documents" folder.
The difference for app folder apps is that the path is automatically translated by Dropbox to be inside the app's own app folder, as opposed to the actual Dropbox root. That's transparent to the app though, and shouldn't be affected by running in Cordova or not.
pardon my ignorance. Is the access key how the API figures out where to put the files then? If so, great
The "app key" identifies the app, and each app is registered to have a specific permission level. An "access token" identifies a particular app-user pair, and allows access to that user's account to the extent allowed by the app's permission. So, based on the app for the app key or access token, Dropbox knows where to put the files (i.e., in an app folder or not).
thanks for the description. makes a lot of sense
One last thing: Is the user access token permanent?
I've been trying to figure out how to read a simple text file on a node server (not in a browser). I can successfully download a JSON file, but a simple text file has me stumped.
Using dropboxClient.filesDownload({path: path}), how do I convert the returned data object into a simple text string? Do I need to use a fileReader? Is there a simple way to do this?
@tracycollins Did you see comment https://github.com/dropbox/dropbox-sdk-js/issues/14#issuecomment-275947685 This works with any text file. I use this for ABC-code.
the pure javascript is
filesDownload({path:"foo.txt"})
.then(function(response){
reader = new FileReader();
reader.addEventListener("loadend", function(){
alert(reader.result);
});
reader.readAsText(response.fileBlob);
})
.catch(function(error){alert(error);})
@bwl21 Yes, I did see the comment but having trouble getting it to work.
The first problem is that dropboxClient.filesDownload returns an object with .fileBinary and not fileBlob as in your comment. Maybe I'm using a different version? Maybe I need to set an option to get a blob?
Then, the response.fileBinary is an array of integers. How do I convert this to text?
I'm a newbie, so excuse my ignorance. Thanks for the help.
filesDownload returns a promise! you need to understand how promises work (see https://developers.google.com/web/fundamentals/primers/promises for an introduction).
Long story short. filesDownload returns immediateley, even if it works asynchroneouslly. You need to pass two methods to the result, one by ".then" which processes the success case, and maybe a .catch for error processing.
In the code example the variable "response" returns an object which has a property called "fileBlob". From this you can read the text using FileReader. This again is asynchroneous and requires a callback method which processes the resulting text.
it may also depend on Dropbox, if the file extension is handled as a textfile.
Yes, I know that it returns a promise.
My code works when I'm downloading a JSON file (I can parse response.fileBinary), but when downloading a text file, the response (in the promise .then) does not have a fileBlob property that I can see.
The response object has the following properties:
name,path_lower,path_display,id,client_modified,server_modified,rev,size,content_hash,fileBinary
It depends on the file exension if dropbox handles a file as textfile. In my case I use ".abc" which I got registered through support at Dropbox.
Maybe your file should have an extension one of the following extensions (https://www.dropbox.com/help/files-folders/file-types-that-preview#text)
.as | .as3 | .asm | .aspx | .bat | .c |
---|---|---|---|---|---|
.cc | .cmake | .coffee | .cpp | .cs | .css |
.cxx | .diff | .erb | .erl | .groovy | .gvy |
.h | .haml | .hh | .hpp | .hxx | .java |
.js | .json | .jsx | .less | .lst | .m |
.make | .markdown | .md | .mdown | .mkdn | .ml |
.mm | .out | .patch | .php | .pl | |
.plist | .properties | .py | .rb | .sass | .scala |
.scm | .script | .scss | .sh | .sml | .sql |
.txt | .vb | .vi | .vim | .xhtml | .xml |
.xsd | .xsl | .yaml | .yml |
DOH!
Turns out all that I needed to do what was: response.fileBinary.toString()
I also did try and error to find the solution above.
@tracycollins I'm glad to see you already got this working. Just to clarify though, the file data is returned in fileBlob
when running in a browser, or fileBinary
when running in node. Beyond that though, it should be the same. That is, regardless of file type, if you're running in node the actual file data will be contained in fileBinary
.