posts - 17, comments - 4, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

[原]Flex上传组件

Posted on 2007-08-28 18:37 愚人 阅读(1255) 评论(0)  编辑  收藏 所属分类: Flex2
自己做了个小小的上传组件雏形,大概的功能有了,还请高手多多指点,嘿嘿

 1<?xml version="1.0" encoding="utf-8"?>
 2<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()" width="400" height="300">
 3    <mx:Script>
 4        <![CDATA[
 5            import mx.controls.Alert;
 6            public var UrlUpload:String ;
 7            public var editable:Boolean = false ;
 8            
 9            private var urlRequest:URLRequest ;
10            private var fileList:Array ;
11            private var fileReferenceList:FileReferenceList ;
12            private var filterArray:Array = [new FileFilter("图片(*.gif;*.png)","*.gif;*.png"),new FileFilter("视频(*.avi;*.rmvb)","*.avi;*.rmvb")] ;
13            private var count:int = 1 ;
14            
15            private function init():void
16            {
17                if( UrlUpload != null )
18                {
19                    urlRequest = new URLRequest(UrlUpload) ;
20                    fileReferenceList = new FileReferenceList() ;
21                    fileReferenceList.addEventListener(Event.SELECT,selectHandler) ;
22                    broswer.addEventListener(MouseEvent.CLICK,broswe);
23                }
24            }
25            
26            private function selectHandler(event:Event):void
27            {
28                fileReferenceList = FileReferenceList(event.target) ;
29                fileList = fileReferenceList.fileList ;
30                filename.text = FileReference(fileList[0]).name ;
31                upup.visible = true ;
32                upup.addEventListener(MouseEvent.CLICK,upload) ;
33            }
34            
35            private function upload(event:Event):void
36            {
37                
38                var fileReference:FileReference = new FileReference() ;
39                for( var i:int = 0; i<fileList.length; i++ )
40                {
41                    fileReference = FileReference(fileList[i]) ;
42                    fileReference.addEventListener(Event.COMPLETE,dotrace);
43                    //可以在这里命名文件field,后台接受时对应html的name,默认为Filedata。方法:fileReference.upload(urlRequest,"filename") ;
44                    fileReference.upload(urlRequest) ;
45                }
46            }
47            
48            private function dotrace(event:Event):void
49            {
50                if( count < fileList.length )
51                {
52                    count++ ;
53                }
54                else
55                {
56                    Alert.show("Upload successfully");
57                    filename.text = null ;
58                    fileList = null ;
59                    upup.visible = false ;
60                    init();
61                }
62            }
63            
64            private function broswe(event:Event):void
65            {
66                fileReferenceList.browse(filterArray) ;
67            }
68        ]]>
69    </mx:Script>
70    <mx:Button x="219" y="113" id="broswer" label="浏览…" fontSize="12"/>
71    <mx:TextInput x="51" editable="{editable}" y="114" id="filename" />
72    <mx:Button label="上传" id="upup" visible="false" horizontalCenter="119" verticalCenter="-26" fontSize="12"/>
73</mx:Canvas>
74


后台数据处理使用PHP,代码很简单

1<?
2$up_dir = "D:\\www\\tmp\\" ;
3move_uploaded_file($_FILES["Filedata"]['tmp_name'],$up_dir.$_FILES['Filedata']['name']) ;
4?>


打算加入自定义过滤属性,现在写死了,呵呵。

只有注册用户登录后才能发表评论。


网站导航: