转自:http://hi.baidu.com/seriawei/item/e7cdbc96b367e8a9cd80e5ce
MXML里的代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"?
?? xmlns:s="library://ns.adobe.com/flex/spark"?
?? xmlns:mx="library://ns.adobe.com/flex/mx" width="250" height="50">
?<s:layout>
??<s:BasicLayout/>
?</s:layout>
?<fx:Declarations>
??<!-- 将非可视元素(例如服务、值对象)放在此处 -->
?</fx:Declarations>
?
?<fx:Script>
??<![CDATA[
???import mx.controls.Alert;
???import mx.events.FlexEvent;
???
???protected function ShowBrose():void
???{
????var file:FileReference=new FileReference();
????file.addEventListener(Event.SELECT,Selected);
????file.addEventListener(ProgressEvent.PROGRESS,Updating);
????file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,Completed);
????file.browse();
???}
???protected function Selected(e:Event):void
???{
????Button_up.enabled=false;
????(e.target as FileReference).upload(new URLRequest("upload.aspx"),(e.target as FileReference).name,false);
???}
???
???protected function Updating(e:ProgressEvent):void
???{
????ProgressBar_pro.setProgress(e.bytesLoaded,e.bytesTotal);
???}
???
???protected function Completed(e:DataEvent):void
???{
????ProgressBar_pro.label="上传完成";?
???}
??]]>
?</fx:Script>
?<mx:ProgressBar x="0" y="7" width="100%" height="34" id="ProgressBar_pro" mode="manual"/>
?<s:Button x="205" y="19" label="上传" click="ShowBrose()" width="44" id="Button_up"/>
</s:Group>
?
ASP.NET "upload.aspx"文件 Page_Load 事件代码:
?protected void Page_Load(object sender,EventArgs e)
??? {
??????? HttpFileCollection files = Request.Files;
??????? if (files.Count == 0)
??????? {
??????????? Response.Write("请不要直接访问该页");
??????????? Response.End();
??????? }
??????? else
??????? {
??????????? HttpPostedFile file = files[0];
??????????? file.SaveAs(Server.MapPath("upfiles") + "/" + Request.Form["fileName"]);
??????? }
??? }
修改上传文件大小限制:
web.config
<system.web> ??<httpRuntime maxRequestLength="123321" executionTimeout="900" /> ?</system.web>