Nhóm :
Member
Tham gia:
16-06-2010
Bài viết:
43
Lần thăm:
308

Browse and Upload Files Using Flash and AS3

In this tutorial you can learn how to use FileReference in Flash ActionScript 3 to add file browse dialogue and uploading to your flash web sites or applications. Your server must process PHP for this to work for you complete. I included my dynamic PHP script that magically renders out a log file also along with the upload just for coding kicks. You may find that writing text files on the fly in PHP is handy know how. You can actually just add a Name and Email input field to the stage and you have yourself a full blown flash form that will allow file upload.

/*
Script Written By: Adam Khoury @ www.developphp.com
*/
// First thing is to set the flashing upload message clip to invisible
uploadMsg.visible = false;
// Set the URL for the PHP uploader script
var URLrequest:URLRequest = new URLRequest("http://www.[yoursite.com].com/uploader_script.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(ProgressEvent.PROGRESS, progressHandler);
// Add event listeners for your 2 buttons
browse_btn.addEventListener(MouseEvent.CLICK, browseBox);
upload_btn.addEventListener(MouseEvent.CLICK, uploadVars);

// Function that fires off when the user presses "browse for a file"
function browseBox(event:MouseEvent):void {
fileRef.browse(allTypes);
}
// Function that fires off when the user presses the "upload it now" btn
function uploadVars(event:MouseEvent):void {
uploadMsg.visible = true;
fileRef.upload(URLrequest);
upload_btn.visible = false;
}
// Function that fires off when File is selected from PC and Browse dialogue box closes
function syncVariables(event:Event):void {
fileDisplay_txt.text = "" + fileRef.name;
blocker.visible = false;
upload_btn.visible = true;
progressBar.width = 2;
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 that fires off when upload is complete
function completeHandler(event:Event):void {
uploadMsg.visible = false;
blocker.visible = true;
status_txt.text = fileRef.name + " has been uploaded.";
fileDisplay_txt.text = "";
}
// 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
progressBar.width = Math.ceil(200*(event.bytesLoaded/event.bytesTotal));
}



http://www.youtube.com/watch?v=9rFdrJOWRq8

Download Source:

http://www.developphp.com/Flash_tutorials/tutorial_files/9rFdrJOWRq8.zip

Ref: http://www.developphp.com/Flash_tutorials/show_tutorial.php?tid=6