The Chooser is the fastest way to get files from Dropbox into your web app. It's a small JavaScript component that enables your app to get files from Dropbox without having to worry about the complexities of implementing a file browser, authentication, or managing uploads and storage.
This tutorial will guide you through everything you need to do to add the Chooser to your app and customize it to suit your use cases.
Click the button below to see the Chooser in action. This demo triggers a Chooser, which lets you choose a file from your Dropbox. Once a file is selected, the link is returned to the host website.
Options: |
|
Link type: | |
Chooser button: | |
Returns: |
Choose something from Dropbox to see the return value
|
The first step in adding the Chooser to your app is to create a Drop-in app. Drop-in apps don't require the production approval step that other Dropbox apps do. You can publish your integration to your users as soon as you're ready.
When you create a Chooser app for the web, you'll need to provide the domain names where your app is hosted. This lets us stop other websites from trying to impersonate your app. If you're developing locally, you can use localhost as the domain name as well.
Once you've created a new app, add the following JavaScript snippet to your HTML. Use the pull-down menu below to select your Dropbox app and the app key will be pre-filled for you.
<script type="text/javascript" src="https://www.dropbox.com/static/api/2/dropins.js" id="dropboxjs" data-app-key="YOUR_APP_KEY"></script>
There are two ways to trigger the Chooser on your website. To create the nice styled button you see in the demo above, you can use the following JavaScript:
var button = Dropbox.createChooseButton(options); document.getElementById("container").appendChild(button);
If you prefer to design a custom button instead, you can trigger the Chooser directly from JavaScript using the following method:
Dropbox.choose(options);
Note that the Chooser opens in a pop-up window, so you should only call this function from within a user-triggered event handler such as a tap or click event. Otherwise, the pop-up will likely be blocked by the browser.
Both methods take a single options
parameter with the following fields:
options = { // Required. Called when a user selects an item in the Chooser. success: function(files) { alert("Here's the file link: " + files[0].link) }, // Optional. Called when the user closes the dialog without selecting a file // and does not include any parameters. cancel: function() { }, // Optional. "preview" (default) is a preview link to the document for sharing, // "direct" is an expiring link to download the contents of the file. For more // information about link types, see Link types below. linkType: "preview", // or "direct" // Optional. A value of false (default) limits selection to a single file, while // true enables multiple file selection. multiselect: false, // or true // Optional. This is a list of file extensions. If specified, the user will // only be able to select files with these extensions. You may also specify // file types, such as "video" or "images" in the list. For more information, // see File types below. By default, all extensions are allowed. extensions: ['.pdf', '.doc', '.docx'], };
The files
parameter in the above success
callback function will be an array of file objects, each containing info about the selected file. If multiselect
is false
, the array will contain a single item. Each file object includes the following fields:
file = { // Name of the file. name: "filename.txt", // URL to access the file, which varies depending on the linkType specified when the // Chooser was triggered. link: "https://...", // Size of the file in bytes. bytes: 464, // URL to a 64x64px icon for the file based on the file's extension. icon: "https://...", // A thumbnail URL generated when the user selects images and videos. // If the user didn't select an image or video, no thumbnail will be included. thumbnailLink: "https://...?bounding_box=75&mode=fit", };
The thumbnail link contains query string parameters that specify how the thumbnail is generated. By modifying these parameters, you can construct URLs for other sizes and modes of thumbnails:
75
(default), 256
, 800
, 1280
, 2048
.The Chooser can be configured to return one of two link types.
In addition to individual file extensions, the extensions parameter in JavaScript allows the following file types: images
, audio
, video
, documents
, and text
. Each file type corresponds to a list of individual file extensions, as defined in the developer guide.
Not all browsers support the Chooser. If a user's browser doesn't support the Chooser, we'll gray out the button and show a warning message if you try to call Dropbox.choose()
. You can check to see if the user's browser is supported by calling Dropbox.isBrowserSupported()
.
The Chooser supports the following browsers: