Archive for March, 2010:
Uploading files with AS3
This example shows how to use the FileReference class to upload a file to a server. Flash can’t upload the file by itself, and instead needs a back-end language to do the server side job.
Actionscript 3
stop();
btn_upload.visible = false
var URLrequest:URLRequest = new URLRequest("http://localhost/upload/upload.php");
// Assign the image types Filter
var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
// Assign the document types filter
var textTypes:FileFilter = new FileFilter("Text Files (*.txt, *.rtf)", "*.txt; *.rtf");
// Add both filter types to an array
var allTypes:Array = new Array(imageTypes, textTypes);
// Set the FileReference name
var fileRef:FileReference = new FileReference();
// Add event listeners for its various fileRef functions below
fileRef.addEventListener(Event.SELECT, syncVariables);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, handleError);
// Add event listeners for your 2 buttons
btn_browse.addEventListener(MouseEvent.CLICK, browseBox);
btn_upload.addEventListener(MouseEvent.CLICK, uploadVars);
// Function that fires off when the user presses "browse"
function browseBox(event:MouseEvent):void {
fileRef.browse(allTypes);
}
// Function that fires off when the user presses the "upload" btn
function uploadVars(event:MouseEvent):void {
fileRef.upload(URLrequest);
}
// Function that fires off when File is selected from PC and Browse dialogue box closes
function syncVariables(event:Event):void {
t_status.text += "" + fileRef.name + "\n";
btn_upload.visible = true;
var variables:URLVariables = new URLVariables();
variables.todayDate = new Date();
variables.Name = "Dude"; // This could be an input field variable like in my contact form tutorial : )
variables.Email = "someDude@someEmail.com"; // This one the same
URLrequest.method = URLRequestMethod.POST;
URLrequest.data = variables;
}
function handleError(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
t_status.text += "Error, please try again...\n"
}
// Function that fires off when upload is complete
function completeHandler(event:Event):void {
t_status.text += fileRef.name + " has been uploaded.\n";
}
// Function that return data when upload is complete
function uploadDataComplete(event:DataEvent):void {
trace("Done")
var xml:XML = new XML(event.data);
trace(xml)
var resultNum = xml.status
if(resultNum == "OK") {
t_status.text += "Finish"
}
}
// Function that fires off when the upload progress begins
function progressHandler(event:ProgressEvent):void {
// we want our progress bar to be 200 pixels wide when done growing so we use 200*
// Set any width using that number, and the bar will be limited to that when done growing
t_status.text += "Uploading... " + Math.ceil(100*(event.bytesLoaded/event.bytesTotal)) + "\n";
}
btn_upload.visible = false
var URLrequest:URLRequest = new URLRequest("http://localhost/upload/upload.php");
// Assign the image types Filter
var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
// Assign the document types filter
var textTypes:FileFilter = new FileFilter("Text Files (*.txt, *.rtf)", "*.txt; *.rtf");
// Add both filter types to an array
var allTypes:Array = new Array(imageTypes, textTypes);
// Set the FileReference name
var fileRef:FileReference = new FileReference();
// Add event listeners for its various fileRef functions below
fileRef.addEventListener(Event.SELECT, syncVariables);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, handleError);
// Add event listeners for your 2 buttons
btn_browse.addEventListener(MouseEvent.CLICK, browseBox);
btn_upload.addEventListener(MouseEvent.CLICK, uploadVars);
// Function that fires off when the user presses "browse"
function browseBox(event:MouseEvent):void {
fileRef.browse(allTypes);
}
// Function that fires off when the user presses the "upload" btn
function uploadVars(event:MouseEvent):void {
fileRef.upload(URLrequest);
}
// Function that fires off when File is selected from PC and Browse dialogue box closes
function syncVariables(event:Event):void {
t_status.text += "" + fileRef.name + "\n";
btn_upload.visible = true;
var variables:URLVariables = new URLVariables();
variables.todayDate = new Date();
variables.Name = "Dude"; // This could be an input field variable like in my contact form tutorial : )
variables.Email = "someDude@someEmail.com"; // This one the same
URLrequest.method = URLRequestMethod.POST;
URLrequest.data = variables;
}
function handleError(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
t_status.text += "Error, please try again...\n"
}
// Function that fires off when upload is complete
function completeHandler(event:Event):void {
t_status.text += fileRef.name + " has been uploaded.\n";
}
// Function that return data when upload is complete
function uploadDataComplete(event:DataEvent):void {
trace("Done")
var xml:XML = new XML(event.data);
trace(xml)
var resultNum = xml.status
if(resultNum == "OK") {
t_status.text += "Finish"
}
}
// Function that fires off when the upload progress begins
function progressHandler(event:ProgressEvent):void {
// we want our progress bar to be 200 pixels wide when done growing so we use 200*
// Set any width using that number, and the bar will be limited to that when done growing
t_status.text += "Uploading... " + Math.ceil(100*(event.bytesLoaded/event.bytesTotal)) + "\n";
}
php
<?php
// Set local PHP vars from the POST vars sent from flash
$todayDate = $_POST['todayDate'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$filename = $_FILES['Filedata']['name'];
$filetmpname = $_FILES['Filedata']['tmp_name'];
$fileType = $_FILES["Filedata"]["type"];
$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);
// Place file on server, into the images folder
move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$filename);
// This section edits your log file, if you don't need a text log file just delete these lines
$myFile = "logFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "\n\ntodayDate: $todayDate \n Name: $Name \n Email: $Email \n ssid: $ssid \n FileName: $filename \n TmpName: $filetmpname \n Type: $fileType \n Size: $fileSizeMB MegaBytes";
fwrite($fh, $stringData);
fclose($fh);
// End log file edit
$message = "<result><status>OK</status><message>$file_name uploaded successfully.</message></result>";
echo $message;
?>
// Set local PHP vars from the POST vars sent from flash
$todayDate = $_POST['todayDate'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$filename = $_FILES['Filedata']['name'];
$filetmpname = $_FILES['Filedata']['tmp_name'];
$fileType = $_FILES["Filedata"]["type"];
$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);
// Place file on server, into the images folder
move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$filename);
// This section edits your log file, if you don't need a text log file just delete these lines
$myFile = "logFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "\n\ntodayDate: $todayDate \n Name: $Name \n Email: $Email \n ssid: $ssid \n FileName: $filename \n TmpName: $filetmpname \n Type: $fileType \n Size: $fileSizeMB MegaBytes";
fwrite($fh, $stringData);
fclose($fh);
// End log file edit
$message = "<result><status>OK</status><message>$file_name uploaded successfully.</message></result>";
echo $message;
?>
2 Comments
Embed youtube video using AS3
This is a quick way to embed youtube videos into Flash.
// This will hold the API player instance once it is initialized.
var player:Object;
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.securityDomain = SecurityDomain.currentDomain;
context.applicationDomain = ApplicationDomain.currentDomain;
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
Security.allowDomain("www.youtube.com")
loader.load(new URLRequest("http://www.youtube.com/v/wrTdpw7_TvE?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
loader.content.addEventListener("onError", onPlayerError);
loader.content.addEventListener("onStateChange", onPlayerStateChange);
loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
}
function onPlayerReady(event:Event):void {
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
// Set appropriate player dimensions for your application
player.setSize(480, 360);
}
function onPlayerError(event:Event):void {
// Event.data contains the event parameter, which is the error code
trace("player error:", Object(event).data);
}
function onPlayerStateChange(event:Event):void {
// Event.data contains the event parameter, which is the new player state
trace("player state:", Object(event).data);
}
function onVideoPlaybackQualityChange(event:Event):void {
// Event.data contains the event parameter, which is the new video quality
trace("video quality:", Object(event).data);
}
var player:Object;
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.securityDomain = SecurityDomain.currentDomain;
context.applicationDomain = ApplicationDomain.currentDomain;
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
Security.allowDomain("www.youtube.com")
loader.load(new URLRequest("http://www.youtube.com/v/wrTdpw7_TvE?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
loader.content.addEventListener("onError", onPlayerError);
loader.content.addEventListener("onStateChange", onPlayerStateChange);
loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
}
function onPlayerReady(event:Event):void {
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
// Set appropriate player dimensions for your application
player.setSize(480, 360);
}
function onPlayerError(event:Event):void {
// Event.data contains the event parameter, which is the error code
trace("player error:", Object(event).data);
}
function onPlayerStateChange(event:Event):void {
// Event.data contains the event parameter, which is the new player state
trace("player state:", Object(event).data);
}
function onVideoPlaybackQualityChange(event:Event):void {
// Event.data contains the event parameter, which is the new video quality
trace("video quality:", Object(event).data);
}
Clear or Delete an Array in AS3
In this example we defined an array called color and fill it with three color name, then to delete and clear out an array using the Slice method AS3.
var color_arr:Array = new Array();
color_arr.push ("Red");
color_arr.push ("Green");
color_arr.push ("Blue");
// clear the array
color_arr.splice(1,1);
trace(color_arr); //Red, Blue
color_arr.push ("Red");
color_arr.push ("Green");
color_arr.push ("Blue");
// clear the array
color_arr.splice(1,1);
trace(color_arr); //Red, Blue
