flex+php 实现的上传
actionscript 部分:
public var file:FileReference;
private var upNum:Number;
public function selectFile():void
{
file = new FileReference();
file.addEventListener(Event.SELECT, fileSelected);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
file.addEventListener(Event.COMPLETE, uploadComplete);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, handleError);
file.browse(getTypes());
}
private function getTypes():Array {
var allTypes:Array = new Array(getImageTypeFilter(), getTextTypeFilter());
return allTypes;
}
private function getImageTypeFilter():FileFilter {
return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
}
private function getTextTypeFilter():FileFilter {
return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
}
public function progressHandler(event:ProgressEvent):void{
trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
upNum=Math.floor((event.bytesLoaded/event.bytesTotal)*100);
probar.label= "uploading" + " " + upNum+ "%";
probar.setProgress(upNum,100);
}
public function handleError(event:IOErrorEvent):void
{
status_txt.text = 'ERROR: ' + event.text + '\n';
}
public function fileSelected(event:Event):void
{
file = FileReference(event.target);
file_txt.text = file.name;
status_txt.text = 'upload file: '+ file.name + '\n';
var request:URLRequest = new URLRequest();
request.url = "theupload.php";
file.upload(request);
}
public function uploadDataComplete(event:DataEvent):void
{
var result:XML = new XML(event.data);
status_txt.text += 'Upload Data Complete\n'
status_txt.text += 'RESULT: ' + result.toString() + '\n'
status_txt.text += 'STATUS: ' + result.status + '\n';
status_txt.text += 'MESSAGE: '+ result.message;
}
public function uploadComplete(event:Event):void
{
status_txt.text += 'Upload complete\n';
}