5
<title>Example: Simple Uploader with Progress Tracking</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Simple Uploader with Progress Tracking</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<p>This example demonstrates how to use the YUI 3 Uploader to send a single image or video (filtered by file extension) to the server and to monitor the upload progress. The example also shows how to use a custom image skin for the "Browse" button used by the Uploader.</p>
23
<p><strong>Please note:</strong> 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. </p>
24
<p><strong>Also note:</strong> 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.</p>
27
<div class="example yui3-skin-sam">
28
<style type="text/css">
33
text-decoration: none;
37
background: url("../assets/uploader/uploadFileButton.png") 0 0 no-repeat;
40
.uploadButton a:visited {
41
background-position: 0 0;
44
.uploadButton a:hover {
45
background-position: 0 -40px;
48
.uploadButton a:active {
49
background-position: 0 -80px;
53
<div id="selectButton" style="width:100px;height:40px"></div>
54
<div class="uploadButton"><a href="#" id="uploadButtonLink"></a></div>
65
YUI({filter:"raw"}).use('uploader', function(Y) {
68
var swfURL = ../../build/uploader/assets/uploader.swf;
72
swfURL += "?t=" + Y.guid();
77
var uploader = new Y.Uploader({boundingBox:"#selectButton",
78
buttonSkin:"../assets/uploader/selectFileButton.png",
83
uploader.on("uploaderReady", setupUploader);
84
uploader.on("fileselect", fileSelect);
85
uploader.on("uploadprogress", updateProgress);
86
uploader.on("uploadcomplete", uploadComplete);
88
Y.one("#uploadButtonLink").on("click", uploadFile);
91
function setupUploader (event) {
92
uploader.set("multiFiles", false);
93
uploader.set("log", true);
95
var fileFilters = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
96
{description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
98
uploader.set("fileFilters", fileFilters);
102
function fileSelect (event) {
103
var fileData = event.fileList;
105
for (var key in fileData) {
106
var output = "File selected: " + fileData[key].name;
107
Y.one("#filename").setContent(output);
111
function updateProgress (event) {
112
Y.one("#percent").setContent("Percent uploaded: " + Math.round((100 * event.bytesLoaded / event.bytesTotal)) + "%");
115
function uploadComplete (event) {
116
Y.one("#percent").setContent("Upload complete!");
119
function uploadFile (event) {
120
uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php");
129
<h2>Set up the Uploader UI</h2>
131
<p>The Uploader requires that the "Browse" button be implemented as an instance of the Flash Player; all other controls can be regular DOM elements. For this example, set up a container for the Flash Player and give it the id <code>selectButton</code> and set up a container for the "Upload" button (<code>uploadButton</code>). In addition, set up containers to display the name of the selected file (<code>filename</code>) and the progress of the upload (<code>percent</code>).</p>
132
<pre class="code prettyprint"><div id="selectButton" style="width:100px;height:40px"></div>
133
<div class="uploadButton"><a href="#" id="uploadButtonLink"></a></div>
135
<div id="filename">
138
<div id="percent">
140
</div></pre>
143
<p>In the head section of the example, define custom styles for the <code>uploadButton</code> to give it an image skin that has multiple button states:</p>
145
<pre class="code prettyprint"><style type="text/css">
150
text-decoration: none;
154
background: url("assets/uploadFileButton.png") 0 0 no-repeat;
157
.uploadButton a:visited {
158
background-position: 0 0;
161
.uploadButton a:hover {
162
background-position: 0 -40px;
165
.uploadButton a:active {
166
background-position: 0 -80px;
168
</style></pre>
171
<h2>Create a YUI Instance</h2>
172
<p>Now that the UI containers and controls are in place, create a YUI instance, using the <code>uploader</code> module, for this example:</p>
174
<pre class="code prettyprint">YUI().use("uploader", function(Y) {
175
// Y is the YUI instance.
176
// The rest of the code in this tutorial is encapsulated
177
// in this anonymous function.
181
<h2>Working around the IE Caching Bug</h2>
182
<p>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,
183
we can prevent loading the SWF from cache by appending a random get variable to the SWF URL:</p>
185
<pre class="code prettyprint">var swfURL = Y.Env.cdn + "uploader/assets/uploader.swf";
187
if (Y.UA.ie >= 6) {
188
swfURL += "?t=" + Y.guid();
192
<h2>Instantiate the Uploader</h2>
194
<p>Next, create an instance of an Uploader and configure it. The Uploader only requires the reference to the container in which the "Browse" button should be placed, but in this example an image skin for the button is being used; as result, we need to provide the <code>buttonSkin</code> property with a reference to the image sprite, as well as the <code>transparent</code> Boolean value (this property specifies whether the transparent areas of the image sprite are rendered as such; if no <code>buttonSkin</code> is specified, the entire uploader would render as transparent). Also note that we are including a custom SWF URL, defined in the workaround above (by default, the SWF URL would be as shown above, but unmodified):</p>
196
<pre class="code prettyprint">var uploader = new Y.Uploader({boundingBox:"#selectButton",
197
buttonSkin:"assets/selectFileButton.png",
202
<h2>Listen for Uploader Events</h2>
203
<p>Next, add event handlers to various uploader events and the <code>click</code> handler for the "Upload" button. The <code>uploaderReady</code> event is particularly important because the uploader may not be ready to accept method calls until this event has fired. Therefore, any method calls and property settings for the uploader should be made within the handler for that event:</p>
204
<pre class="code prettyprint">uploader.on("uploaderReady", setupUploader);
205
uploader.on("fileselect", fileSelect);
206
uploader.on("uploadprogress", updateProgress);
207
uploader.on("uploadcomplete", uploadComplete);
209
Y.one("#uploadButtonLink").on("click", uploadFile);</pre>
212
<h2>Set Up the Uploader</h2>
213
<p>Once the uploader is ready, and the <code>uploaderReady</code> event is fired, set properties to further configure the Uploader. In particular, set the <code>multiFiles</code> property to restrict user to selecting only a single file, the <code>log</code> property to provide debug information (output only if the computer is running the debug version of the Flash player), and the <code>fileFilters</code> property to filter files in the user's "Browse" dialog:</p>
215
<pre class="code prettyprint">function setupUploader (event) {
216
uploader.set("multiFiles", false);
217
uploader.set("log", true);
219
var fileFilters = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
220
{description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
222
uploader.set("fileFilters", fileFilters);
226
<h2>Handler for the <code>fileselect</code> Event</h2>
228
<p>In the handler for the <code>fileselect</code>, extract the file list from the event payload and display the name of the file. In this particular case, the list should only contain one file name, since the user was restricted to selecting a single file:</p>
230
<pre class="code prettyprint">function fileSelect (event) {
231
var fileData = event.fileList;
233
for (var key in fileData) {
234
var output = "File selected: " + fileData[key].name;
235
Y.one("#filename").setContent(output);
240
<h2>Handler for Other Events</h2>
242
<p>In the <code>uploadprogress</code> handler, update the content of the <code>percent</code> container based on the values provided in the event payload. In the <code>uploadcomplete</code> handler, place the final message into the <code>percent</code> container. Finally, in the <code>click</code> handler, initiate the upload to a specific URL:</p>
244
<pre class="code prettyprint">function updateProgress (event) {
245
Y.one("#percent").setContent("Percent uploaded: " + Math.round((100 * event.bytesLoaded / event.bytesTotal)) + "%");
248
function uploadComplete (event) {
249
Y.one("#percent").setContent("Upload complete!");
252
function uploadFile (event) {
253
uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php");
259
<h2>Full Source Code For this Example</h2>
261
<pre class="code prettyprint"><style type="text/css">
269
border: 2px solid #c4c4c4;
271
-moz-border-radius: 5px;
272
-webkit-border-radius:5px;
274
.yui3-progressbar .yui3-progressbar-content {
275
background-color:#fff;
277
/* width: 200px; */
279
.yui3-progressbar .yui3-progressbar-label {
284
font-family:Arial,Helvetica,sans-serif;
286
.yui3-progressbar .yui3-progressbar-slider {
287
background-color:#e0bb30;
293
#selectFilesLink #selectLink,
294
#uploadFilesLink #uploadLink {
296
text-decoration: underline;
300
<div id="uploaderContainer">
301
<div id="uploaderOverlay" style="position:absolute; z-index:2"></div>
302
<div id="selectFilesLink" style="z-index:1"><a id="selectLink" href="#">Select Files</a></div>
304
<div id="uploadFilesLink"><a id="uploadLink" href="#">Upload Files</a></div>
306
<div id="files">
307
<table id="filenames" style="border-width:1px; border-style:solid; padding:5px;">
309
<tr><th>Filename</th><th>File size</th><th>Percent uploaded</th></tr>
318
YUI({filter:"raw", gallery: 'gallery-2011.02.09-21-32'}).use("uploader", 'gallery-progress-bar', function(Y) {
324
var overlayRegion = Y.one("#selectLink").get('region');
325
Y.one("#uploaderOverlay").set("offsetWidth", overlayRegion.width);
326
Y.one("#uploaderOverlay").set("offsetHeight", overlayRegion.height);
330
var swfURL = ../../build/uploader/assets/uploader.swf;
333
if (Y.UA.ie >= 6) {
334
swfURL += "?t=" + Y.guid();
337
uploader = new Y.Uploader({boundingBox:"#uploaderOverlay",
340
uploader.on("uploaderReady", setupUploader);
341
uploader.on("fileselect", fileSelect);
342
uploader.on("uploadprogress", updateProgress);
343
uploader.on("uploadcomplete", uploadComplete);
346
Y.on("domready", init);
349
function setupUploader (event) {
350
uploader.set("multiFiles", true);
351
uploader.set("simLimit", 3);
352
uploader.set("log", true);
354
var fileFilters = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
355
{description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
357
uploader.set("fileFilters", fileFilters);
360
function fileSelect (event) {
361
var fileData = event.fileList;
363
for (var key in fileData) {
364
if (!selectedFiles[fileData[key].id]) {
365
var output = "<tr><td>" + fileData[key].name + "</td><td>" +
366
fileData[key].size + "</td><td><div id='div_" +
367
fileData[key].id + "' class='progressbars'></div></td></tr>";
368
Y.one("#filenames tbody").append(output);
370
var progressBar = new Y.ProgressBar({id:"pb_" + fileData[key].id, layout : '<div class="{labelClass}"></div><div class="{sliderClass}"></div>'});
371
progressBar.render("#div_" + fileData[key].id);
372
progressBar.set("progress", 0);
374
selectedFiles[fileData[key].id] = true;
380
function updateProgress (event) {
381
var pb = Y.Widget.getByNode("#pb_" + event.id);
382
pb.set("progress", Math.round(100 * event.bytesLoaded / event.bytesTotal));
385
function uploadComplete (event) {
386
var pb = Y.Widget.getByNode("#pb_" + event.id);
387
pb.set("progress", 100);
390
function uploadFiles (event) {
391
uploader.uploadAll("http://www.yswfblog.com/upload/upload_simple.php");
394
Y.one("#uploadFilesLink").on("click", uploadFiles);
398
</script></pre>
403
<div id="sidebar" class="yui3-u">
407
<div class="sidebox">
409
<h2 class="no-toc">Examples</h2>
413
<ul class="examples">
416
<li data-description="How to upload a single file, while tracking progress and using a sprite-skinned button">
417
<a href="uploader-simple.html">Simple Uploader with Progress Tracking</a>
422
<li data-description="How to upload a single file, along with GET and POST Vars Submission and receive data from the server">
423
<a href="uploader-withvars.html">Single File Upload with Additional Data</a>
428
<li data-description="How to upload multiple files with progress tracking and a transparent overlay upload button">
429
<a href="uploader-multiple.html">Multiple Files Upload with Transparent Overlay Button</a>
443
<script src="../assets/vendor/prettify/prettify-min.js"></script>
444
<script>prettyPrint();</script>