Example: Multiple Files Upload with Transparent Overlay Button

In this example, the Uploader is used to send multiple images or videos (filtered by file extension) 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.

Also note: The uploader is not supported on iOS devices (iPhone and iPad), because Flash player is not available on that system. This example will not function on such devices.

FilenameFile sizePercent uploaded

Setting 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 directly 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):

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

Creating a YUI Instance

Now that the UI containers and controls are in place, create a YUI instance. Since this example uses the Progress-bar 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 use statement:

YUI({
  gallery: 'gallery-2011.02.09-21-32'
   }).use("uploader", 
          "gallery-progress-bar", 
          function(Y) {
				// Y is the YUI instance.
				// The rest of the code in this tutorial is encapsulated 
				// in this anonymous function.
		  });

Working around the IE Caching Bug

Due to a bug in IE6 through IE8, when a SWF is loaded from local cache (after a page has been reloaded, for example), it's unable to properly communicate with the JavaScript VM. For that reason, specifically for IE, we can prevent loading the SWF from cache by appending a random get variable to the SWF URL:

var swfURL = Y.Env.cdn + "uploader/assets/uploader.swf";

if (Y.UA.ie >= 6) {
    swfURL += "?t=" + Y.guid();
}

Instantiate the Uploader

Next, create an instance of the Uploader. Make sure to include the custom swfURL we constructed as part of the workaround above. 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. In addition, create a variable to store selected files, to be used later, in the fileSelect handler:

var uploader,
    selectedFiles = {};
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", swfURL: swfURL});	

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:

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 ids, so they can be easily updated when upload progress information becomes available. Since the event payload always carries the total list of selected files, keep track of which files have already been selected, so that they don't get displayed in the table twice:

function fileSelect (event) {
	var fileData = event.fileList;	
    
	for (var key in fileData) {
	        if (!selectedFiles[fileData[key].id]) {
			   var output = "<tr><td>" + fileData[key].name + "</td><td>" + 
			                fileData[key].size + "</td><td><div id='div_" + 
			                fileData[key].id + "' class='progressbars'></div></td></tr>";
			   Y.one("#filenames tbody").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);
               
               selectedFiles[fileData[key].id] = true;
			}
	}

}

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

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

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

Full Source Code For this Example

<style type="text/css">

.progressbars {
	width:300px;
}

.yui3-progressbar {
	margin-bottom:3px;
	border: 2px solid #c4c4c4;
	border-radius:5px;
	-moz-border-radius: 5px;
	-webkit-border-radius:5px;
}
.yui3-progressbar .yui3-progressbar-content {
	background-color:#fff;
	position:relative;
/*	width: 200px; */
}
.yui3-progressbar .yui3-progressbar-label {
	position: absolute;
	top:1px;
	left:3px;
	font-size:11px;
	font-family:Arial,Helvetica,sans-serif;
}
.yui3-progressbar .yui3-progressbar-slider {
	background-color:#e0bb30;
	height: 15px;
	line-height: 29px;
	width: 0;
}

#selectFilesLink #selectLink,
#uploadFilesLink #uploadLink {
    color: #00c;
    text-decoration: underline;
}
</style>

<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;">
    <thead>
	   <tr><th>Filename</th><th>File size</th><th>Percent uploaded</th></tr>
	</thead>
	<tbody>
	</tbody>
  </table>	
</div>

<script>

YUI({filter:"raw", gallery: 'gallery-2011.02.09-21-32'}).use("uploader-deprecated", 'gallery-progress-bar', function(Y) {
	
var uploader,
    selectedFiles = {};

function init () {
var overlayRegion = Y.one("#selectLink").get('region');
Y.one("#uploaderOverlay").set("offsetWidth", overlayRegion.width);
Y.one("#uploaderOverlay").set("offsetHeight", overlayRegion.height);


 
var swfURL = ../../build/uploader-deprecated/assets/uploader.swf; 


if (Y.UA.ie >= 6) {
	swfURL += "?t=" + Y.guid();
}

uploader = new Y.Uploader({boundingBox:"#uploaderOverlay", 
                           swfURL: swfURL});	

uploader.on("uploaderReady", setupUploader);
uploader.on("fileselect", fileSelect);
uploader.on("uploadprogress", updateProgress);
uploader.on("uploadcomplete", uploadComplete);
}

Y.on("domready", init);


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

function fileSelect (event) {
	var fileData = event.fileList;	
    
	for (var key in fileData) {
	        if (!selectedFiles[fileData[key].id]) {
			   var output = "<tr><td>" + fileData[key].name + "</td><td>" + 
			                fileData[key].size + "</td><td><div id='div_" + 
			                fileData[key].id + "' class='progressbars'></div></td></tr>";
			   Y.one("#filenames tbody").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);
               
               selectedFiles[fileData[key].id] = true;
			}
	}

}

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

function uploadComplete (event) {
	var pb = Y.Widget.getByNode("#pb_" + event.id);
	pb.set("progress", 100);
}

function uploadFiles (event) {
	uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php");
}

Y.one("#uploadFilesLink").on("click", uploadFiles);

});

</script>