5
<title>Example: Single File Upload with Additional Data</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: Single File Upload with Additional Data</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<p>This example extends the single file upload example and shows how to submit GET and POST variables along with the file upload request, and how to receive data from the server. The uploader is set up to filter extensions of uploaded files to only allow common image and video formats.</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>
62
<div>Sending via GET: foo='bar', foo1='bar1'</div>
63
<div>Sending via POST: bar='bazz', bar1='bazz1'</div>
64
<div id="result">Data returned from the server:</div>
68
YUI({filter:"raw"}).use('uploader', function (Y) {
71
var swfURL = ../../build/uploader/assets/uploader.swf;
75
swfURL += "?t=" + Y.guid();
78
var uploader = new Y.Uploader({boundingBox:"#selectButton",
79
buttonSkin:"../assets/uploader/selectFileButton.png",
84
uploader.on("uploaderReady", setupUploader);
85
uploader.on("fileselect", fileSelect);
86
uploader.on("uploadprogress", updateProgress);
87
uploader.on("uploadcomplete", uploadComplete);
88
uploader.on("uploadcompletedata", uploadCompleteData);
90
Y.one("#uploadButtonLink").on("click", uploadFile);
93
function setupUploader (event) {
94
uploader.set("multiFiles", false);
95
uploader.set("log", true);
97
var fileFilters = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
98
{description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
100
uploader.set("fileFilters", fileFilters);
104
function fileSelect (event) {
105
var fileData = event.fileList;
107
for (var key in fileData) {
108
var output = "File selected: " + fileData[key].name;
109
Y.one("#filename").setContent(output);
113
function updateProgress (event) {
114
Y.one("#percent").setContent("Percent uploaded: " + Math.round((100 * event.bytesLoaded / event.bytesTotal)) + "%");
117
function uploadComplete (event) {
118
Y.one("#percent").setContent("Upload complete!");
121
function uploadCompleteData (event) {
122
Y.one("#result").setContent("Data returned from the server:<br>" + event.data);
125
function uploadFile (event) {
126
uploader.uploadAll("http://www.yswfblog.com/upload/upload.php?foo=bar&foo1=bar1", "POST", {bar:"bazz", bar1:"bazz1"});
135
<h2>Adding Functionality to Simple File Upload</h2>
137
<p>Please read the "Simple File Upload" example tutorial first, since this example builds on top of it.</p>
139
<h2>Additional UI</h2>
140
<p>In addition to the UI for selecting files, uploading them, and reporting on the upload progress, add the container to output the data returned from the server:</p>
142
<pre class="code prettyprint"><div id="result">Data returned from the server:</div></pre>
145
<h2>Event Binding for <code>uploadcompletedata</code></h2>
146
<p>When declaring uploader event bindings, add a handler for the <code>uploadcompletedata</code> event, which carries the output returned by the server:</p>
148
<pre class="code prettyprint">uploader.on("uploadcompletedata", uploadCompleteData);</pre>
151
<h2>Handling <code>uploadcompletedata</code></h2>
152
<p>Add a handler for the <code>uploadcompletedata</code> event. In the handler, set the content of the <code>result</code> container to the server's response to the request, carried in the event payload:</p>
154
<pre class="code prettyprint">function uploadCompleteData (event) {
155
Y.one("#result").setContent("Data returned from the server:<br>" + event.data);
159
<h2>Change Upload Method</h2>
160
<p>Finally, modify the <code>uploadAll()</code> method call to specifically set the variable transmission method to POST and to add the variables to be carried in the POST request:</p>
162
<pre class="code prettyprint">function uploadFile (event) {
163
uploader.uploadAll("http://www.yswfblog.com/upload/upload.php?foo=bar&foo1=bar1", "POST", {bar:"bazz", bar1:"bazz1"});
167
<h2>Full Source Code For this Example</h2>
169
<pre class="code prettyprint"><style type="text/css">
174
text-decoration: none;
178
background: url("../assets/uploader/uploadFileButton.png") 0 0 no-repeat;
181
.uploadButton a:visited {
182
background-position: 0 0;
185
.uploadButton a:hover {
186
background-position: 0 -40px;
189
.uploadButton a:active {
190
background-position: 0 -80px;
194
<div id="selectButton" style="width:100px;height:40px"></div>
195
<div class="uploadButton"><a href="#" id="uploadButtonLink"></a></div>
197
<div id="filename">
200
<div id="percent">
203
<div>Sending via GET: foo='bar', foo1='bar1'</div>
204
<div>Sending via POST: bar='bazz', bar1='bazz1'</div>
205
<div id="result">Data returned from the server:</div>
209
YUI({filter:"raw"}).use('uploader', function (Y) {
212
var swfURL = ../../build/uploader/assets/uploader.swf;
215
if (Y.UA.ie >= 6) {
216
swfURL += "?t=" + Y.guid();
219
var uploader = new Y.Uploader({boundingBox:"#selectButton",
220
buttonSkin:"../assets/uploader/selectFileButton.png",
225
uploader.on("uploaderReady", setupUploader);
226
uploader.on("fileselect", fileSelect);
227
uploader.on("uploadprogress", updateProgress);
228
uploader.on("uploadcomplete", uploadComplete);
229
uploader.on("uploadcompletedata", uploadCompleteData);
231
Y.one("#uploadButtonLink").on("click", uploadFile);
234
function setupUploader (event) {
235
uploader.set("multiFiles", false);
236
uploader.set("log", true);
238
var fileFilters = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
239
{description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
241
uploader.set("fileFilters", fileFilters);
245
function fileSelect (event) {
246
var fileData = event.fileList;
248
for (var key in fileData) {
249
var output = "File selected: " + fileData[key].name;
250
Y.one("#filename").setContent(output);
254
function updateProgress (event) {
255
Y.one("#percent").setContent("Percent uploaded: " + Math.round((100 * event.bytesLoaded / event.bytesTotal)) + "%");
258
function uploadComplete (event) {
259
Y.one("#percent").setContent("Upload complete!");
262
function uploadCompleteData (event) {
263
Y.one("#result").setContent("Data returned from the server:<br>" + event.data);
266
function uploadFile (event) {
267
uploader.uploadAll("http://www.yswfblog.com/upload/upload.php?foo=bar&foo1=bar1", "POST", {bar:"bazz", bar1:"bazz1"});
273
</script></pre>
279
<div id="sidebar" class="yui3-u">
283
<div class="sidebox">
285
<h2 class="no-toc">Examples</h2>
289
<ul class="examples">
292
<li data-description="How to upload a single file, while tracking progress and using a sprite-skinned button">
293
<a href="uploader-simple.html">Simple Uploader with Progress Tracking</a>
298
<li data-description="How to upload a single file, along with GET and POST Vars Submission and receive data from the server">
299
<a href="uploader-withvars.html">Single File Upload with Additional Data</a>
304
<li data-description="How to upload multiple files with progress tracking and a transparent overlay upload button">
305
<a href="uploader-multiple.html">Multiple Files Upload with Transparent Overlay Button</a>
319
<script src="../assets/vendor/prettify/prettify-min.js"></script>
320
<script>prettyPrint();</script>