YUI 3.x Home -

YUI Library Examples: Uploader: Multiple File Upload with Progress Tracking and a Transparent Overlay Upload Button

Uploader: Multiple File Upload with Progress Tracking and a Transparent Overlay Upload Button

In this example, the Uploader is used to send multiple files to the server and monitor their upload progress with individual progress bars. The example also demonstrates the use of the transparent overlay "Browse" button for the Uploader.

Please note: This example will not work when run from a local filesystem because Flash only allows ExternalInterface communication with pages loaded from the network. If you’d like to run this example locally, set up a local web server and launch it from there.

FilenameFile sizePercent uploaded

Multiple File Upload with Progress Tracking and a Transparent Overlay Upload Button

Set up Uploader UI

In this example, the UI for the Uploader consists of two links, as well as containers for displaying various information about the upload process. Since the Uploader uses Flash, which requires direct user input to show the "Browse" dialog, the "Select Files" link cannot be used to invoke that dialog. Instead, create a container that overlays this link (uploaderOverlay): the instance of Flash player will be placed in that container and made full transparent, thus accepting user input but providing the visual information from the link below it. In addition, create a table for holding the list of selected files (filenames):

  1. <div id="uploaderContainer">
  2. <div id="uploaderOverlay" style="position:absolute; z-index:2"></div>
  3. <div id="selectFilesLink" style="z-index:1"><a id="selectLink" href="#">Select Files</a></div>
  4. </div>
  5. <div id="uploadFilesLink"><a id="uploadLink" href="#">Upload Files</a></div>
  6.  
  7. <div id="files">
  8. <table id="filenames" style="border-width:1px; border-style:solid; padding:5px;">
  9. <tr><td>Filename</td><td>File size</td><td>Percent uploaded</td></tr>
  10. </table>
  11. </div>
<div id="uploaderContainer"> 
	<div id="uploaderOverlay" style="position:absolute; z-index:2"></div> 
	<div id="selectFilesLink" style="z-index:1"><a id="selectLink" href="#">Select Files</a></div> 
</div> 
<div id="uploadFilesLink"><a id="uploadLink" href="#">Upload Files</a></div>
 
<div id="files">
  <table id="filenames" style="border-width:1px; border-style:solid; padding:5px;">
	<tr><td>Filename</td><td>File size</td><td>Percent uploaded</td></tr>
  </table>	
</div>

Create a YUI Instance

Now that the UI containers and controls are in place, create a YUI instance. Since this example uses the Progressbar module from YUI Gallery (built by Anthony Pipkin), we also include the gallery config option among the YUI() arguments, as well as 'gallery-progressbar' in the usestatement:

  1. YUI({gallery: 'gallery-2010.06.30-19-54'}).use("uploader", "gallery-progressbar", function(Y) {
  2. // Y is the YUI instance.
  3. // The rest of the code in this tutorial is encapsulated
  4. // in this anonymous function.
  5. } );
YUI({gallery: 'gallery-2010.06.30-19-54'}).use("uploader", "gallery-progressbar", function(Y) {
	// Y is the YUI instance.
	// The rest of the code in this tutorial is encapsulated 
	// in this anonymous function.
} );

Instantiate the Uploader

Next, create an instance of the Uploader. Since the Uploader is being placed in a relatively-sized container, subscribe to the domready event and instantiate the uploader in its handler. After instantiating it, add handlers to the uploader events:

  1. var uploader;
  2. Y.on("domready", init);
  3.  
  4. function init () {
  5.  
  6. var overlayRegion = Y.one("#selectLink").get('region');
  7. Y.log(overlayRegion);
  8. Y.one("#uploaderOverlay").set("offsetWidth", overlayRegion.width);
  9. Y.one("#uploaderOverlay").set("offsetHeight", overlayRegion.height);
  10.  
  11. uploader = new Y.Uploader({boundingBox:"#uploaderOverlay"});
  12.  
  13. uploader.on("uploaderReady", setupUploader);
  14. uploader.on("fileselect", fileSelect);
  15. uploader.on("uploadprogress", updateProgress);
  16. uploader.on("uploadcomplete", uploadComplete);
  17.  
  18. Y.one("#uploadFilesLink").on("click", uploadFile);
  19. }
var uploader;
Y.on("domready", init);
 
function init () {
 
var overlayRegion = Y.one("#selectLink").get('region');
Y.log(overlayRegion);
Y.one("#uploaderOverlay").set("offsetWidth", overlayRegion.width);
Y.one("#uploaderOverlay").set("offsetHeight", overlayRegion.height);
 
uploader = new Y.Uploader({boundingBox:"#uploaderOverlay"});	
 
uploader.on("uploaderReady", setupUploader);
uploader.on("fileselect", fileSelect);
uploader.on("uploadprogress", updateProgress);
uploader.on("uploadcomplete", uploadComplete);
 
Y.one("#uploadFilesLink").on("click", uploadFile);
}

Configure the Uploader

Once the uploader is ready, the setupUploader function is called, and Uploader configuration can be set. For this example, set the multiFiles property to true, allowing the user to select multiple files. Also set the simLimit property to 3, indicating that three files can be uploaded simultaneously. We also set the log property (which allows to see debug output from Flash, only available if running the Flash Debug player), and the fileFilters property, which filters the user's "Browse" dialog to only include files with certain extensions:

  1. function setupUploader (event) {
  2. uploader.set("multiFiles", true);
  3. uploader.set("simLimit", 3);
  4. uploader.set("log", true);
  5.  
  6. var fileFilters = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
  7. {description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
  8.  
  9. uploader.set("fileFilters", fileFilters);
  10. }
function setupUploader (event) {
	uploader.set("multiFiles", true);
	uploader.set("simLimit", 3);
	uploader.set("log", true);
 
	var fileFilters = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
	                   {description:"Videos", extensions:"*.avi;*.mov;*.mpg"}); 
 
    uploader.set("fileFilters", fileFilters); 
}

fileselect Event Handler

When the user selects files to be uploaded, display the list of selected files in a table. Populate the table with data from the event payload, and reserve a column to display a progress bar for each individual file. Set the initial value of the progress bars to 0, and provide them with custom id's, so they can be easily updated when upload progress information becomes available.

  1. function fileSelect(event) {
  2. Y.log("File was selected, parsing...");
  3. var fileData = event.fileList;
  4.  
  5. for (var key in fileData) {
  6. var output = "<tr><td>" + fileData[key].name + "</td><td>" + fileData[key].size + "</td><td><div id='div_" + fileData[key].id + "'></div></td></tr>\n";
  7. Y.one("#filenames").append(output);
  8.  
  9. var progressBar = new Y.ProgressBar({id:"pb_" + fileData[key].id, layout : '<div class="{labelClass}"></div><div class="{sliderClass}"></div>'});
  10. progressBar.render("#div_" + fileData[key].id);
  11. progressBar.set("progress", 0);
  12. }
  13. }
function fileSelect(event) {
	Y.log("File was selected, parsing...");
	var fileData = event.fileList;	
 
	for (var key in fileData) {
		var output = "<tr><td>" + fileData[key].name + "</td><td>" + fileData[key].size + "</td><td><div id='div_" + fileData[key].id + "'></div></td></tr>\n";
		Y.one("#filenames").append(output);
 
		var progressBar = new Y.ProgressBar({id:"pb_" + fileData[key].id, layout : '<div class="{labelClass}"></div><div class="{sliderClass}"></div>'});
		progressBar.render("#div_" + fileData[key].id);
		progressBar.set("progress", 0);
	}
}

uploadprogress Event Handler

When uploadprogress events arrive, update relevant progress bars with the current progress information (note that the event payload contains the id of the file for which the upload progress information is being made available):

  1. function updateProgress (event) {
  2. var pb = Y.Widget.getByNode("#pb_" + event.id);
  3. pb.set("progress", Math.round(100 * event.bytesLoaded / event.bytesTotal));
  4. }
function updateProgress (event) {
	var pb = Y.Widget.getByNode("#pb_" + event.id);
	pb.set("progress", Math.round(100 * event.bytesLoaded / event.bytesTotal));
}

Other Event Handlers

Once the upload completes, make sure that the specific progress bar has reached 100 percent. Also include the handler for the click event on the upload button, which kicks off the upload process (since the uploadAll() method is being used, the queue will be managed automatically):

  1. function uploadComplete (event) {
  2. var pb = Y.Widget.getByNode("#pb_" + event.id);
  3. pb.set("progress", 100);
  4. }
  5.  
  6. function uploadFile (event) {
  7. uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php");
  8. }
function uploadComplete (event) {
	var pb = Y.Widget.getByNode("#pb_" + event.id);
	pb.set("progress", 100);
}
 
function uploadFile (event) {
	uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php");
}

Copyright © 2011 Yahoo! Inc. All rights reserved.

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