uploader-multiple.mustache revision c1cc0c913c4ed9ac3de73aaadd3d0db93d2240df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<div class="intro">
<p>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.</p>
<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>
<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>
</div>
<div class="example yui3-skin-sam">
{{>uploader-multiple-source}}
</div>
<h2>Setting up Uploader UI</h2>
<p>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`):</p>
```
<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>
```
<h2>Creating a YUI Instance</h2>
<p>Now that the UI containers and controls are in place, create a YUI instance. Since this example uses the <a href="http://yuilibrary.com/gallery/show/progress-bar">Progress-bar</a> module from <a href="http://www.yuilibrary.com/gallery">YUI Gallery</a> (built by <a href="http://github.com/apipkin/">Anthony Pipkin</a>), we also include the gallery config option among the YUI() arguments, as well as `gallery-progressbar` in the `use` statement: </p>
```
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.
});
```
<h2>Working around the IE Caching Bug</h2>
<p>Due to a bug in IE7 and 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:</p>
```
var swfURL = Y.Env.cdn + "uploader/assets/uploader.swf";
if (Y.UA.ie >= 7) {
swfURL += "?t=" + Y.guid();
}
```
<h2>Instantiate the Uploader</h2>
<p>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:</p>
```
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", swfURL: swfURL});
uploader.on("uploaderReady", setupUploader);
uploader.on("fileselect", fileSelect);
uploader.on("uploadprogress", updateProgress);
uploader.on("uploadcomplete", uploadComplete);
Y.one("#uploadFilesLink").on("click", uploadFile);
}
```
<h2>Configure the Uploader</h2>
<p>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: </p>
```
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);
}
```
<h2>`fileselect` Event Handler</h2>
<p>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.</p>
```
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);
}
}
```
<h3>`uploadprogress` Event Handler</h3>
<p>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):</p>
```
function updateProgress (event) {
var pb = Y.Widget.getByNode("#pb_" + event.id);
pb.set("progress",
}
```
<h2>Other Event Handlers</h2>
<p>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):</p>
```
function uploadComplete (event) {
var pb = Y.Widget.getByNode("#pb_" + event.id);
pb.set("progress", 100);
}
function uploadFile (event) {
}
```
<h2>Full Source Code For this Example</h2>
```
{{>uploader-multiple-source}}
```