Advanced Uploader Example With Transparent UI and Automatic Queue Management : Uploader « YUI Library « JavaScript DHTML






Advanced Uploader Example With Transparent UI and Automatic Queue Management

 


<!--
Software License Agreement (BSD License)
Copyright (c) 2009, Yahoo! Inc.
All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the
      following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of Yahoo! Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Yahoo! Inc.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sources of Intellectual Property Included in the YUI Library

YUI is issued by Yahoo! under the BSD license above. Below is a list of certain publicly available software that is the source of intellectual property in YUI, along with the licensing terms that pertain to thosesources of IP. This list is for informational purposes only and is not intended to represent an exhaustive list of third party contributions to the YUI.

    * Douglas Crockford's JSON parsing and stringifying methods: In the JSON Utility, Douglas Crockford's JSON parsing and stringifying methods are adapted from work published at JSON.org. The adapted work is in the public domain.
    * Robert Penner's animation-easing algorithms: In the Animation Utility, YUI makes use of Robert Penner's algorithms for easing.
    * Geoff Stearns's SWFObject: In the Charts Control and the Uploader versions through 2.7.0, YUI makes use of Geoff Stearns's SWFObject v1.5 for Flash Player detection and embedding. More information on SWFObject can be found here (http://blog.deconcept.com/swfobject/). SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License (http://www.opensource.org/licenses/mit-license.php).
    * Diego Perini's IEContentLoaded technique: The Event Utility employs a technique developed by Diego Perini and licensed under GPL. YUI's use of this technique is included under our BSD license with the author's permission.

-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>


    <meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Advanced Uploader Example With Transparent UI and Automatic Queue Management</title>

<style type="text/css">
/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
  margin:0;
  padding:0;
}
</style>

<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" />
<script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/uploader/uploader.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

<!--there is no custom header content for this example-->

</head>

<body class=" yui-skin-sam">


<h1>Advanced Uploader Example With Transparent UI and Automatic Queue Management</h1>

<div class="exampleIntro">
  <p>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.</p>

<p><strong>Note:</strong> The YUI Uploader Control requires Flash Player 9.0.45 or higher. The latest version of Flash Player is available at the <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player Download Center</a>.</p>
<p><strong>Note:</strong> 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.</p>
<p><strong>Note:</strong> 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.      
</div>

<!--BEGIN SOURCE CODE FOR EXAMPLE  -->

<style>
#selectFilesLink a, #uploadFilesLink a, #clearFilesLink a {
  color: #0000CC;
  background-color: #FFFFFF;
}

#selectFilesLink a:visited, #uploadFilesLink a:visited, #clearFilesLink a:visited {
  color: #0000CC;
  background-color: #FFFFFF;
}

#uploadFilesLink a:hover, #clearFilesLink a:hover {  
  color: #FFFFFF;
  background-color: #000000;
}
</style>

<div id="uiElements" style="display:inline;">
    <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" onClick="upload(); return false;" href="#">Upload Files</a></div>
</div>

<div id="simUploads"> Number of simultaneous uploads:
  <select id="simulUploads">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
</div>

<div id="dataTableContainer"></div>

<script type="text/javascript">

YAHOO.util.Event.onDOMReady(function () { 
var uiLayer = YAHOO.util.Dom.getRegion('selectLink');
var overlay = YAHOO.util.Dom.get('uploaderOverlay');
YAHOO.util.Dom.setStyle(overlay, 'width', uiLayer.right-uiLayer.left + "px");
YAHOO.util.Dom.setStyle(overlay, 'height', uiLayer.bottom-uiLayer.top + "px");
});

  // Custom URL for the uploader swf file (same folder).
  YAHOO.widget.Uploader.SWFURL = "yui_2.7.0b-assets/uploader-assets/uploader.swf";

    // Instantiate the uploader and write it to its placeholder div.
  var uploader = new YAHOO.widget.Uploader( "uploaderOverlay" );
  
  // Add event listeners to various events on the uploader.
  // Methods on the uploader should only be called once the 
  // contentReady event has fired.
  
  uploader.addListener('contentReady', handleContentReady);
  uploader.addListener('fileSelect', onFileSelect)
  uploader.addListener('uploadStart', onUploadStart);
  uploader.addListener('uploadProgress', onUploadProgress);
  uploader.addListener('uploadCancel', onUploadCancel);
  uploader.addListener('uploadComplete', onUploadComplete);
  uploader.addListener('uploadCompleteData', onUploadResponse);
  uploader.addListener('uploadError', onUploadError);
    uploader.addListener('rollOver', handleRollOver);
    uploader.addListener('rollOut', handleRollOut);
    uploader.addListener('click', handleClick);
      
    // Variable for holding the filelist.
  var fileList;
  
  // When the mouse rolls over the uploader, this function
  // is called in response to the rollOver event.
  // It changes the appearance of the UI element below the Flash overlay.
  function handleRollOver () {
    YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'color', "#FFFFFF");
    YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'background-color', "#000000");
  }
  
  // On rollOut event, this function is called, which changes the appearance of the
  // UI element below the Flash layer back to its original state.
  function handleRollOut () {
    YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'color', "#0000CC");
    YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'background-color', "#FFFFFF");
  }
  
  // When the Flash layer is clicked, the "Browse" dialog is invoked.
  // The click event handler allows you to do something else if you need to.
  function handleClick () {
  }
  
  // When contentReady event is fired, you can call methods on the uploader.
  function handleContentReady () {
      // Allows the uploader to send log messages to trace, as well as to YAHOO.log
    uploader.setAllowLogging(true);
    
    // Allows multiple file selection in "Browse" dialog.
    uploader.setAllowMultipleFiles(true);
    
    // New set of file filters.
    var ff = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
                       {description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
                       
    // Apply new set of file filters to the uploader.
    uploader.setFileFilters(ff);
  }

  // Actually uploads the files. In this case,
  // uploadAll() is used for automated queueing and upload 
  // of all files on the list.
  // You can manage the queue on your own and use "upload" instead,
  // if you need to modify the properties of the request for each
  // individual file.
  function upload() {
  if (fileList != null) {
    uploader.setSimUploadLimit(parseInt(document.getElementById("simulUploads").value));
    uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php", "POST", null, "Filedata");
  }  
  }
  
  // Fired when the user selects files in the "Browse" dialog
  // and clicks "Ok".
  function onFileSelect(event) {
    if(YAHOO.lang.hasOwnProperty(event, fileList) && event.fileList != null) {
      fileList = event.fileList;
      createDataTable(fileList);
    }
  }

  function createDataTable(entries) {
    rowCounter = 0;
    this.fileIdHash = {};
    this.dataArr = [];
    for(var i in entries) {
       var entry = entries[i];
     entry["progress"] = "<div style='height:5px;width:100px;background-color:#CCC;'></div>";
       dataArr.unshift(entry);
    }
  
    for (var j = 0; j < dataArr.length; j++) {
      this.fileIdHash[dataArr[j].id] = j;
    }
  
      var myColumnDefs = [
          {key:"name", label: "File Name", sortable:false},
         {key:"size", label: "Size", sortable:false},
         {key:"progress", label: "Upload progress", sortable:false}
      ];

    this.myDataSource = new YAHOO.util.DataSource(dataArr);
    this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
      this.myDataSource.responseSchema = {
          fields: ["id","name","created","modified","type", "size", "progress"]
      };

    this.singleSelectDataTable = new YAHOO.widget.DataTable("dataTableContainer",
             myColumnDefs, this.myDataSource, {
                 caption:"Files To Upload",
                 selectionMode:"single"
             });
  }

    // Do something on each file's upload start.
  function onUploadStart(event) {
  
  }
  
  // Do something on each file's upload progress event.
  function onUploadProgress(event) {
    rowNum = fileIdHash[event["id"]];
    prog = Math.round(100*(event["bytesLoaded"]/event["bytesTotal"]));
    progbar = "<div style='height:5px;width:100px;background-color:#CCC;'><div style='height:5px;background-color:#F00;width:" + prog + "px;'></div></div>";
    singleSelectDataTable.updateRow(rowNum, {name: dataArr[rowNum]["name"], size: dataArr[rowNum]["size"], progress: progbar});  
  }
  
  // Do something when each file's upload is complete.
  function onUploadComplete(event) {
    rowNum = fileIdHash[event["id"]];
    prog = Math.round(100*(event["bytesLoaded"]/event["bytesTotal"]));
    progbar = "<div style='height:5px;width:100px;background-color:#CCC;'><div style='height:5px;background-color:#F00;width:100px;'></div></div>";
    singleSelectDataTable.updateRow(rowNum, {name: dataArr[rowNum]["name"], size: dataArr[rowNum]["size"], progress: progbar});
  }
  
  // Do something if a file upload throws an error.
  // (When uploadAll() is used, the Uploader will
  // attempt to continue uploading.
  function onUploadError(event) {

  }
  
  // Do something if an upload is cancelled.
  function onUploadCancel(event) {

  }
  
  // Do something when data is received back from the server.
  function onUploadResponse(event) {

  }
</script>



<!--END SOURCE CODE FOR EXAMPLE  -->

</body>
</html>
<!-- presentbright.corp.yahoo.com uncompressed/chunked Thu Feb 19 10:53:20 PST 2009 -->

   
  








yui_2.7.0b.zip( 4,431 k)

Related examples in the same category

1.Advanced Uploader Example With Cookie Submission as a POST variable
2.Advanced Uploader Example With Additional POST Variables and Server Data Return
3.Simple Uploader Example With Button UI