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.
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.
1 | <style> |
2 | #selectFilesLink a, #uploadFilesLink a, #clearFilesLink a { |
3 | color: #0000CC; |
4 | background-color: #FFFFFF; |
5 | } |
6 | |
7 | #selectFilesLink a:visited, #uploadFilesLink a:visited, #clearFilesLink a:visited { |
8 | color: #0000CC; |
9 | background-color: #FFFFFF; |
10 | } |
11 | |
12 | #uploadFilesLink a:hover, #clearFilesLink a:hover { |
13 | color: #FFFFFF; |
14 | background-color: #000000; |
15 | } |
16 | </style> |
view plain | print | ? |
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.
1 | <div id="uiElements" style="display:inline;"> |
2 | <div id="uploaderContainer"> |
3 | <div id="uploaderOverlay" style="position:absolute; z-index:2"></div> |
4 | <div id="selectFilesLink" style="z-index:1"><a id="selectLink" href="#">Select Files</a></div> |
5 | </div> |
6 | <div id="uploadFilesLink"><a id="uploadLink" onClick="upload(); return false;" href="#">Upload Files</a></div> |
7 | </div> |
view plain | print | ? |
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.
1 | <div id="simUploads"> Number of simultaneous uploads: |
2 | <select id="simulUploads"> |
3 | <option value="1">1</option> |
4 | <option value="2">2</option> |
5 | <option value="3">3</option> |
6 | <option value="4">4</option> |
7 | </select> |
8 | </div> |
9 | |
10 | <div id="dataTableContainer"></div> |
view plain | print | ? |
Once the DOM is ready, we can size our container for the transparent UI to the link below it. The following code accomplishes that:
1 | <script type="text/javascript"> |
2 | |
3 | YAHOO.util.Event.onDOMReady(function () { |
4 | var uiLayer = YAHOO.util.Dom.getRegion('selectLink'); |
5 | var overlay = YAHOO.util.Dom.get('uploaderOverlay'); |
6 | YAHOO.util.Dom.setStyle(overlay, 'width', uiLayer.right-uiLayer.left + "px"); |
7 | YAHOO.util.Dom.setStyle(overlay, 'height', uiLayer.bottom-uiLayer.top + "px"); |
8 | }); |
view plain | print | ? |
Now we can instantiate the uploader and place it in the container div.
1 | YAHOO.widget.Uploader.SWFURL = "assets/uploader.swf"; |
2 | var uploader = new YAHOO.widget.Uploader( "uploaderOverlay" ); |
view plain | print | ? |
We add handler functions to the uploader events. Note that methods on the uploader should not be called until the "contentReady" event has fired:
1 | uploader.addListener('contentReady', handleContentReady); |
2 | uploader.addListener('fileSelect', onFileSelect) |
3 | uploader.addListener('uploadStart', onUploadStart); |
4 | uploader.addListener('uploadProgress', onUploadProgress); |
5 | uploader.addListener('uploadCancel', onUploadCancel); |
6 | uploader.addListener('uploadComplete', onUploadComplete); |
7 | uploader.addListener('uploadCompleteData', onUploadResponse); |
8 | uploader.addListener('uploadError', onUploadError); |
9 | uploader.addListener('rollOver', handleRollOver); |
10 | uploader.addListener('rollOut', handleRollOut); |
11 | uploader.addListener('click', handleClick); |
12 | uploader.addListener('mouseDown', handleMouseDown); |
13 | uploader.addListener('mouseUp', handleMouseUp); |
view plain | print | ? |
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.
1 | function handleRollOver () { |
2 | YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'color', "#FFFFFF"); |
3 | YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'background-color', "#000000"); |
4 | } |
5 | |
6 | function handleRollOut () { |
7 | YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'color', "#0000CC"); |
8 | YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'background-color', "#FFFFFF"); |
9 | } |
view plain | print | ? |
These are placeholders for the rest of the mouse event handlers: mouseDown, mouseUp, and click.
1 | function handleMouseDown () { |
2 | } |
3 | |
4 | function handleMouseUp () { |
5 | } |
6 | |
7 | function handleClick () { |
8 | } |
view plain | print | ? |
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.
1 | function handleContentReady () { |
2 | // Allows the uploader to send log messages to trace, as well as to YAHOO.log |
3 | uploader.setAllowLogging(true); |
4 | |
5 | // Allows multiple file selection in "Browse" dialog. |
6 | uploader.setAllowMultipleFiles(true); |
7 | |
8 | // New set of file filters. |
9 | var ff = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"}, |
10 | {description:"Videos", extensions:"*.avi;*.mov;*.mpg"}); |
11 | |
12 | // Apply new set of file filters to the uploader. |
13 | uploader.setFileFilters(ff); |
14 | } |
view plain | print | ? |
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.
1 | var fileList; |
2 | |
3 | function onFileSelect(event) { |
4 | if('fileList' in event && event.fileList != null) { |
5 | fileList = event.fileList; |
6 | createDataTable(fileList); |
7 | } |
8 | } |
view plain | print | ? |
This function renders the data table based on the data provided in the file list.
1 | function createDataTable(entries) { |
2 | rowCounter = 0; |
3 | this.fileIdHash = {}; |
4 | this.dataArr = []; |
5 | for(var i in entries) { |
6 | var entry = entries[i]; |
7 | entry["progress"] = "<div style='height:5px;width:100px;background-color:#CCC;'></div>"; |
8 | dataArr.unshift(entry); |
9 | } |
10 | |
11 | for (var j = 0; j < dataArr.length; j++) { |
12 | this.fileIdHash[dataArr[j].id] = j; |
13 | } |
14 | |
15 | var myColumnDefs = [ |
16 | {key:"name", label: "File Name", sortable:false}, |
17 | {key:"size", label: "Size", sortable:false}, |
18 | {key:"progress", label: "Upload progress", sortable:false} |
19 | ]; |
20 | |
21 | this.myDataSource = new YAHOO.util.DataSource(dataArr); |
22 | this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; |
23 | this.myDataSource.responseSchema = { |
24 | fields: ["id","name","created","modified","type", "size", "progress"] |
25 | }; |
26 | |
27 | this.singleSelectDataTable = new YAHOO.widget.DataTable("dataTableContainer", |
28 | myColumnDefs, this.myDataSource, { |
29 | caption:"Files To Upload", |
30 | selectionMode:"single" |
31 | }); |
32 | } |
view plain | print | ? |
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().
1 | function upload() { |
2 | if (fileList != null) { |
3 | uploader.setSimUploadLimit(parseInt(document.getElementById("simulUploads").value)); |
4 | uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php"); |
5 | } |
6 | } |
view plain | print | ? |
As uploadProgress events are fired, we render a progress bar in the corresponding row of the DataTable.
1 | function onUploadProgress(event) { |
2 | rowNum = fileIdHash[event["id"]]; |
3 | prog = Math.round(100*(event["bytesLoaded"]/event["bytesTotal"])); |
4 | progbar = "<div style='height:5px;width:100px;background-color:#CCC;'><div style='height:5px;background-color:#F00;width:" + prog + "px;'></div></div>"; |
5 | singleSelectDataTable.updateRow(rowNum, {name: dataArr[rowNum]["name"], size: dataArr[rowNum]["size"], progress: progbar}); |
6 | } |
view plain | print | ? |
When an upload completes, we fully render the corresponding progress bar.
1 | function onUploadComplete(event) { |
2 | rowNum = fileIdHash[event["id"]]; |
3 | prog = Math.round(100*(event["bytesLoaded"]/event["bytesTotal"])); |
4 | progbar = "<div style='height:5px;width:100px;background-color:#CCC;'><div style='height:5px;background-color:#F00;width:100px;'></div></div>"; |
5 | singleSelectDataTable.updateRow(rowNum, {name: dataArr[rowNum]["name"], size: dataArr[rowNum]["size"], progress: progbar}); |
6 | } |
view plain | print | ? |
These are placeholders for handlers of various other events dispatched by the uploader.
1 | function onUploadStart(event) { |
2 | } |
3 | |
4 | function onUploadError(event) { |
5 | } |
6 | |
7 | function onUploadCancel(event) { |
8 | } |
9 | |
10 | function onUploadResponse(event) { |
11 | } |
12 | </script> |
view plain | print | ? |
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.
INFO 238ms (+124) 10:24:49 AM:
LogReader instance0
LogReader initialized
INFO 114ms (+113) 10:24:49 AM:
global
Unable to load SWF assets/uploader.swf
INFO 1ms (+0) 10:24:49 AM:
global
Logger initialized
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings