AMFPHP
/**
* Copyright ericyang ( http://wonderfl.net/user/ericyang )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/olmW
*/
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.NetConnection;
import flash.net.Responder;
import flash.utils.ByteArray;
import flash.display.Sprite;
import flash.text.TextField;
import flash.net.FileReference;
import flash.display.Loader;
public class FlashTest extends Sprite {
private var resp1:Responder;
private var resp2:Responder;
private var nc:NetConnection;
private var textbtn:TextField = new TextField();
private var fileRef:FileReference = new FileReference();
private var loader:Loader;
public function FlashTest() {
// write as3 code here..
textbtn.text = "BROWSE";
var sp:Sprite = new Sprite()
sp.graphics.beginFill(0xff00ff,0.8);
sp.graphics.drawRect(0,0,100,50);
sp.graphics.endFill();
sp.addChild(textbtn);
addChild(sp);
sp.buttonMode = true;
sp.addEventListener(MouseEvent.CLICK, onClickHandler);
}
private function onClickHandler(e:MouseEvent):void
{
fileRef.browse();
fileRef.addEventListener(Event.SELECT, function():void{
fileRef.load();
fileRef.addEventListener(Event.COMPLETE, onCompleteHandler);
});
}
private function onCompleteHandler(e:Event):void
{
textbtn.text = typeof (fileRef.data);
loader = new Loader();
loader.loadBytes(fileRef.data);
loader.addEventListener(Event.ADDED, loaderCompleteHandler);
}
private function loaderCompleteHandler(e:Event):void
{
loader.width = 400;
loader.height = 300;
addChild(loader);
textbtn.text = "UPLOAD";
var sp:Sprite = new Sprite()
sp.graphics.beginFill(0xff00ff,0.8);
sp.graphics.drawRect(0,0,100,50);
sp.graphics.endFill();
sp.addChild(textbtn);
addChild(sp);
sp.y = loader.height;
sp.buttonMode = true;
sp.addEventListener(MouseEvent.CLICK, uploadHandler);
}
public function senddata():void
{
nc = new NetConnection();
nc.connect("http://ericyang.er.funpic.org/amfphp/gateway.php");
resp1 = new Responder(onSave, onBadReturn);
var ba:ByteArray = new ByteArray();
ba = fileRef.data as ByteArray;
//a.writeObject(allAuthors);
// send bytearray to database
nc.call("more.upload", resp1, ba, fileRef.name);
textbtn.text = "UPLOADING";
}
private function uploadHandler(e:Event):void
{
senddata();
}
// when you retrieve bytearray from server
// once bytearray has been sent to server
private function onSave(o:Object):void
{
// if bytearray was inserted into database
textbtn.text = "Done";
}
// bad amfphp call
private function onBadReturn(o:Object):void
{
textbtn.text = "Error";
}
}
}