Flex简单文件上传

FileReferenceExample.as:

package oreilly.cookbook{
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.FileFilter;
    import flash.net.FileReference;
    import flash.net.URLRequest;
    import mx.controls.Button;
    import mx.containers.Canvas;

    public class FileReferenceExample extends Canvas {
        private var uploadURL:URLRequest;
        private var file:FileReference;
        private var button:Button;

        public function FileReferenceExample() {
            uploadURL = new URLRequest();
            uploadURL.url = "http://localhost:3003/UploadTest.aspx";
            file = new FileReference();
            configureListeners(file);
        }
  
  protected function btnUpload_Click(event:MouseEvent):void{
   file.browse(getTypes());
  }
  
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.CANCEL, cancelHandler);
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(Event.SELECT, selectHandler);
        }

        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");
        }

        private function cancelHandler(event:Event):void {
            trace("cancelHandler: " + event);
        }

        private function completeHandler(event:Event):void {
            trace("completeHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }
       
        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            var file:FileReference = FileReference(event.target);
            trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function selectHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
            file.upload(uploadURL);
        }
    }
}


FileReferenceExample.mxml:

<?xml version="1.0" encoding="utf-8"?>
<cookbook:FileReferenceExample xmlns:cookbook="oreilly.cookbook.*" xmlns:mx="http://www.adobe.com/2006/mxml" width="347" height="208">
 <mx:Button id=”btnUpload” label="Button" x="10" y="10" click="btnUpload_Click(event);"/>
</cookbook:FileReferenceExample>

这种codebehind模式在as脚本里是无法操纵mxml界面元素的.
需要在as脚本里操纵界面元素,不要使用这种继承关系,直接在mxml里引用as脚本<mx:Script source="FileReferenceExample_cb.as"/>

你可能感兴趣的:(Flex)