YUI 3.x Home -

YUI Library Examples: Uploader: Single File Upload with GET and POST Vars Submission and Server Data Return

Uploader: Single File Upload with GET and POST Vars Submission and Server Data Return

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.

Please note: 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.

File selected:
Percent uploaded:
Sending via GET: foo='bar', foo1='bar1'
Sending via POST: bar='bazz', bar1='bazz1'
Data returned from the server:

Single File Upload with GET and POST Vars Submission and Server Data Return

Adding Functionality to Single File Upload

Please read the "Single File Upload" example tutorial first, since this example builds on top of it.

Additional UI

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:

  1. <div id="result">Data returned from the server:</div>
<div id="result">Data returned from the server:</div>

Event Binding for uploadcompletedata

When declaring uploader event bindings, add a handler for the uploadcompletedata event, which carries the output returned by the server:

  1. uploader.on("uploadcompletedata", uploadCompleteData);
uploader.on("uploadcompletedata", uploadCompleteData);

Handling uploadcompletedata

Add a handler for the uploadcompletedata event. In the handler, set the content of the result container to the server's response to the request, carried in the event payload:

  1. function uploadCompleteData (event) {
  2. Y.one("#result").setContent("Data returned from the server:<br>" + event.data);
  3. }
function uploadCompleteData (event) {
	Y.one("#result").setContent("Data returned from the server:<br>" + event.data);
}

Change Upload Method

Finally, modify the uploadAll() method call to specifically set the variable transmission method to POST and to add the variables to be carried in the POST request:

  1. function uploadFile (event) {
  2. uploader.uploadAll("http://www.yswfblog.com/upload/upload.php?foo=bar&foo1=bar1", "POST", {bar:"bazz", bar1:"bazz1"});
  3. }
function uploadFile (event) {
	uploader.uploadAll("http://www.yswfblog.com/upload/upload.php?foo=bar&foo1=bar1", "POST", {bar:"bazz", bar1:"bazz1"});
}

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings