dropbox-sdk-js icon indicating copy to clipboard operation
dropbox-sdk-js copied to clipboard

Add complex examples

Open rileytomasek opened this issue 8 years ago • 34 comments

We will want to include more complex examples, as well as examples that show how to obtain an access token.

rileytomasek avatar Apr 12 '16 23:04 rileytomasek

+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 avatar Jun 10 '16 20:06 greg-db

@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 avatar Aug 15 '16 21:08 rileytomasek

@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 and path.not_file from /download) but that may be related to #50.
  • using filesListFolderContinue along withfilesListFolder.
  • using upload sessions.
  • sharing a folder.

greg-db avatar Aug 15 '16 21:08 greg-db

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 avatar Sep 05 '16 13:09 amk221

@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.

greg-db avatar Sep 06 '16 16:09 greg-db

Right, totally missed that! Cheers. So that lets me download a file, but not build a hyperlink to that file though...

amk221 avatar Sep 06 '16 16:09 amk221

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.

elburz avatar Oct 17 '16 12:10 elburz

Thanks for the additional feedback @elburz !

greg-db avatar Oct 17 '16 18:10 greg-db

I would appreciate an example of

  • how to show the progress of an upload
  • how to do an in-browser view of a file

kennellink avatar Dec 01 '16 05:12 kennellink

Thanks for the feedback @kennellink !

greg-db avatar Dec 01 '16 18:12 greg-db

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

bwl21 avatar Jan 14 '17 13:01 bwl21

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
});

bwl21 avatar Jan 29 '17 19:01 bwl21

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 avatar Jan 29 '17 21:01 ppietak

@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.

bwl21 avatar Jan 29 '17 21:01 bwl21

Thanks for the feedback all!

greg-db avatar Jan 30 '17 18:01 greg-db

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.

brownieboy avatar Feb 11 '17 00:02 brownieboy

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 avatar May 25 '17 16:05 tommck

@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.

greg-db avatar May 25 '17 18:05 greg-db

pardon my ignorance. Is the access key how the API figures out where to put the files then? If so, great

tommck avatar May 25 '17 18:05 tommck

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).

greg-db avatar May 25 '17 18:05 greg-db

thanks for the description. makes a lot of sense

One last thing: Is the user access token permanent?

tommck avatar May 25 '17 18:05 tommck

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 avatar Aug 22 '18 17:08 tracycollins

@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 avatar Aug 22 '18 17:08 bwl21

@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.

tracycollins avatar Aug 22 '18 18:08 tracycollins

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.

bwl21 avatar Aug 22 '18 18:08 bwl21

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

tracycollins avatar Aug 22 '18 18:08 tracycollins

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

bwl21 avatar Aug 22 '18 18:08 bwl21

DOH!

Turns out all that I needed to do what was: response.fileBinary.toString()

tracycollins avatar Aug 22 '18 18:08 tracycollins

I also did try and error to find the solution above.

bwl21 avatar Aug 22 '18 18:08 bwl21

@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.

greg-db avatar Aug 22 '18 18:08 greg-db