当前位置:首页 > Flex+asp.net上传文件

Flex+asp.net上传文件

点击次数:744  更新日期:2010-12-29
\n

前台Flex文件:UploadSample.mxml,其代码如下所示:

\n

<!–

\n

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

\n

–> 1 <?xml version=”1.0″ encoding=”utf-8″?>
2 <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
3 <mx:Style>
4 global
5 {
6 fontSize : 12;
7 }
8 </mx:Style>
9
10 <mx:Script>
11 <![CDATA[
12 // 先搞 1 个 FileReference
13 private var file:FileReference = new FileReference();
14
15 // 上传状态指示, 和下面的文本框绑定
16 [Bindable]
17 private var stateText:String = “请选择一个文件上传”;
18
19 // createChildren 比 creationComplete 事件更早发生, 省的注册事件侦听, 直接在这里写了
20 protected override function createChildren():void
21 {
22 super.createChildren();
23 file.addEventListener(Event.SELECT, file_select);
24 //file.addEventListener(Event.COMPLETE, file_complete);
25 file.addEventListener(ProgressEvent.PROGRESS, file_progress);
26 file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, file_complete);
27 }
28
29 // 选择 1 个文件的事件
30 private function file_select (e:Event):void
31 {
32 stateText = “选择了文件 ” + file.name;
33 }
34
35 // 上传完毕后的事件
36 private function file_complete (e:Event):void
37 {
38 stateText = “上传完毕”;
39 }
40
41 private function file_progress (e:ProgressEvent):void
42 {
43 stateText = “已上传 ” + Math.round(100 * e.bytesLoaded / e.bytesTotal) + “%”;
44 }
45 // 先判断一下文件大小, 再上传, FileService.aspx 就是上传地址
46 private function upload ():void
47 {
48 if (file.size > 0)
49 {
50 stateText = “正在上传 ” + file.name;
51 var request:URLRequest =
52 new URLRequest(“http://localhost:1851/WebSite1/FileService.aspx”);
53 file.upload(request);
54 }
55 }
56
57
58 ]]>
59 </mx:Script>
60
61 <mx:Panel width=”250″ height=”112″ layout=”vertical” title=”上传示例”
62 verticalAlign=”middle” horizontalAlign=”center” >
63 <mx:HBox>
64 <mx:TextInput text=”{stateText}” width=”160″ editable=”false”/>
65 <mx:Button label=”浏览” click=”file.browse();”/>
66 </mx:HBox>
67 <mx:HBox>
68 <mx:Button label=”上传” click=”upload();”/>
69 </mx:HBox>
70 </mx:Panel>
71 </mx:Application>
72

\n