Tuesday, August 22, 2006

Upload File AS3 PHP

Example for upload file with As3.
  1. Open a new document with Flash 9 and assign as document class [your package].Example
  2. Create in your package the file Example.as and add this code:

    package com.duemas.leonardo{
    import flash.display.MovieClip;
    import flash.events.*;
    import com.duemas.leonardo.io.FileManager;
    public class Example extends MovieClip{

    public const IMPORT_IMAGE:String="importImage";
    private var fileManager:FileManager;
    public function Example(){
    createInstance();
    registerListeners();
    startApp()
    }
    private function createInstance(){
    fileManager =new FileManager();
    }
    private function registerListeners(){
    this.addEventListener(IMPORT_IMAGE,fileManager.importImage);
    fileManager.addEventListener(fileManager.ON_IMPORT_IMAGE,onImport);
    }
    private function onImport(ev:Event):void{
    var f:FileManager=FileManager(ev.target);
    var img_uploaded:String=f.lastFileSelect;
    trace("image uploaded: "+img_uploaded);
    }
    private function startApp(){
    var e =new Event(IMPORT_IMAGE);
    dispatchEvent(e);
    }
    }
    }
  3. Create in your package the file FileManager.as and add this code:

    package com.duemas.leonardo.io
    {
    import flash.events.*;
    import flash.net.*;
    import com.duemas.leonardo.constant.Constant;
    import flash.events.*
    public class FileManager
    {
    public const ON_IMPORT_IMAGE:String="OnImportImage";
    private var f:FileReference;
    public var lastFileSelect:String;
    private var imagesFilter:FileFilter;

    public function FileManager()
    {
    super();
    imagesFilter= new FileFilter("Images", "*.jpg;*.gif;*.png")
    }
    public function importImage(ev:Event){
    f=new FileReference();
    f.addEventListener(Event.SELECT,onFileSelect);
    f.addEventListener(IOErrorEvent.IO_ERROR,errorFile);
    f.addEventListener(Event.COMPLETE, uploadComplete);
    f.browse([imagesFilter])
    }
    private function errorFile(e:IOErrorEvent){
    trace("error "+e.toString())
    }
    private function uploadComplete(e:Event){
    lastFileSelect=f.name;
    var ev:Event=new Event(ON_IMPORT_IMAGE);
    dispatchEvent(ev);
    }
    private function onFileSelect(e:Event):void{
    var file:FileReference = FileReference(e.target);
    trace("selectHandler: " + file.name );
    var uploadUrl:URLRequest = new URLRequest();
    uploadUrl.url = Constant.SERVER_URL+Constant.PHP_URL+Constant.UPLOAD_URL;
    file.upload(uploadUrl)
    }
    }
    }
  4. Create in your package the file Constant.as and add this code:

    package com.duemas.leonardo.constant
    {

    public class Constant
    {
    public static const SERVER_URL:String="http://localhost/leonardo/";
    public static const PHP_URL:String="php/";
    public static const UPLOAD_URL:String="upload.php";
    public static const IMAGE_FOLDER:String="img/";
    }

    }
  5. Create in path SERVER_URL + PHP_URL the file upload.php open and close php tags and add this code:

    $destination_dir = '../img/';
    if(
    isset($_FILES['Filedata']) &&
    is_array($_FILES['Filedata']) &&
    isset(
    $_FILES['Filedata']['tmp_name'],
    $_FILES['Filedata']['name'],
    $_FILES['Filedata']['size'],
    $_FILES['Filedata']['error']
    ) &&
    intVal($_FILES['Filedata']['error']) === 0
    ) {
    if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $destination_dir.$_FILES['Filedata']['name'])) {
    $result = "
    Date: ".date('Y-m-d H:i:s')."
    File: {$_FILES['Filedata']['name']}
    Size: {$_FILES['Filedata']['size']}
    Successfull uploaded.
    ";
    }
    else {
    $result = "
    Date: ".date('Y-m-d H:i:s')."
    File: {$_FILES['Filedata']['name']}
    Size: {$_FILES['Filedata']['size']}
    Error: {$_FILES['Filedata']['error']}
    Unable to move file.
    ";
    }
    if(@$fp = fopen($destination_dir.'upload.txt', 'w')) {
    fwrite($fp, $result);
    fclose($fp);
    }
    }

Load and Display with as3

Forget loadMovie, now for load image u use Loader class


package{
import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
public class Example extends Sprite {
function Example(){loadImage("bdog.jpg");}
public function loadImage(u:String){
var _loader:Loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
var request:URLRequest = new URLRequest(u);
_loader.load(request);
addChild(_loader);
}
private function onComplete(ev:Event):void{
var loader:LoaderInfo = LoaderInfo(ev.target);
}
}
}

How waste your time - Compiling AS3

I waste a lot of time with a compiling error:

Error #1046: Type was not found or was not a compile-time constant: DataManager.
private var dm:DataManager;

With the new virtual machine u must declare public the class!!!

public class DataManager extends UIObject