使用Flex上传文件

时间:2013-08-25 19:29:44

标签: actionscript-3 flex flex4 flex4.5

我的Flex应用程序中有以下代码:

        protected function btn_select_image_clickHandler(event:MouseEvent):void {
            fileRef.addEventListener(Event.SELECT, Image_Select);
            fileRef.addEventListener(Event.COMPLETE, completeHandler);
            fileRef.browse(UploadFilter);
        }
        private function Image_Select(e:Event) : void {

            var fileSize:Number = Math.round( fileRef.size / 1024 );

            if ( fileSize <= 500 ) {

                var request:URLRequest  = new URLRequest( "http://gestor.domain.com/upload_image.php" );
                try {
                    fileRef.upload( request );
                } catch(error:Error) {
                    Alert.show( "Error loading image.", "Error" );
                }

            } else {
                Alert.show( "Image higher than 500Kb.", "Warning" );
            }
        }

        private function completeHandler(e:Event) : void {

            var file:FileReference = FileReference( e.target );
            Alert.show( "Data returned: "+file.data);
            if ( file.data != null ) {
                _filerefUploadMode = false;
                fileRef.load();
            }

        }

php文件只回显一个字符串,以便在compleHandler触发时显示。

事实是file.data只返回 null ,我不知道发生了什么。

我缺少吗?

1 个答案:

答案 0 :(得分:0)

首先添加此导入:

import flash.events.DataEvent;

然后添加此侦听器:

fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);

然后使用这样的处理程序:

private function uploadCompleteHandler(e:DataEvent) : void 
{
    trace (e.data);
}