YUI Library Home

YUI Library Examples: Uploader Control: Advanced Uploader Example With Transparent UI and Automatic Queue Management

Uploader Control: Advanced Uploader Example With Transparent UI and Automatic Queue Management

This example demonstrates how the Uploader can be rendered as a transparent layer on top of your own UI, and how the upload queue of multiple files can be managed automatically.

Note: The YUI Uploader Control requires Flash Player 9.0.45 or higher. The latest version of Flash Player is available at the Adobe Flash Player Download Center.

Note: The YUI Uploader Control requires the uploader.swf Flash file that is distributed as part of the YUI package, in the uploader/assets folder. Copy the uploader.swf to your server and set the YAHOO.Uploader.SWFURL variable to its full path.

Note: This example uses a server-side script to accept file uploads. The script used does not open or store any data sent to it. Nonetheless, when trying out the example, do not send any sensitive or private data. Do not exceed file size of 10 MB.

Number of simultaneous uploads:

Advanced Uploader Example with Transparent Rendering and Multiple File Upload

In this example, we allow the user to select multiple images and videos, and upload them to the server, while tracking progress on the upload. The user can also choose how many files to upload simultaneously. Because of security changes in Flash Player 10, the UI for invoking the "Browse" dialog has to be contained within the Flash player. In this example, the Flash player is rendered as a transparent overlay on top of a custom HTML-based UI. The Uploader running in the Flash player dispatches necessary mouse events to the DOM to make visual changes to the overlaid UI.

For starters, let's define the necessary UI styles. We will use regular text links as the UI, and switch their background color when the mouse hovers over them.

Next, we'll place the UI elements on the page. Notice that the selectFilesLink div is overlaid by the uploaderOverlay div. uploaderOverlay is where we'll place the transparent Flash UI layer, that will dispatch various mouse events, based on which we will be able to change the appearance of the UI below.

We also add a dropdown for setting the number of simultaneous uploads, and a container for the YUI DataTable that will display the list of uploaded files and upload progress.

Once the DOM is ready, we can size our container for the transparent UI to the link below it. The following code accomplishes that:

Now we can instantiate the uploader and place it in the container div.

We add handler functions to the uploader events. Note that methods on the uploader should not be called until the "contentReady" event has fired:

These handlers are called when the mouse rolls over and out of the uploader, respectively. They modify the appearance of the UI layer under the transparent Flash layer to match the behavior of the rest of the UI.

These are placeholders for the rest of the mouse event handlers: mouseDown, mouseUp, and click.

When contentReady event fires, we can configure the uploader to our needs. Here, we allow transmission of log messages (sent both to YUI Logger and to Flash trace), allow the user to select multiple files, and specify file extension filters to include in the 'Browse' dialog.

This function is called when files are selected and fileSelect event is fired. It uses a helper function, defined below, to render a DataTable displaying a list of files, their sizes, and space to display the upload progress.

This function renders the data table based on the data provided in the file list.

In this function, we actually initiate the upload. We set the number of files to upload simultaneously, using the value from the dropdown UI, and use the automatic queue management by calling uploadAll().

As uploadProgress events are fired, we render a progress bar in the corresponding row of the DataTable.

When an upload completes, we fully render the corresponding progress bar.

These are placeholders for handlers of various other events dispatched by the uploader.

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings