Flex2.0實現檔案上傳功能(伺服器為ASP.NET)

來源:互聯網
上載者:User
簡介
        新的Flex2.0類庫裡提供了檔案類,方便了上傳/下載檔案。下面的程式demo示範了Flex2.0產生flash來訪問本地檔案,在flash裡上傳使用者選擇的檔案到伺服器,flash用戶端可以處理檔案上傳進度等多個事件,伺服器端是C#寫的檔案接收模組,把使用者上傳的檔案儲存在伺服器上。
        Demo示範了ProgressEventType.PROGRESS, EventType.SELECT 2個事件的處理方法。

測試效果

測試環境

作業系統:windows2003 Server
Flex版本:Flex 2.0 Alpha 1
Flash版本: flash Player 8.5
WEB伺服器:
          IIS 6.0
         .net FrameWork 1.1

用戶端代碼:FileUpload.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml"
    xmlns="*" creationComplete="init();">
    <mx:Script>
        <![CDATA[
            import flash.net.FileReference;
            import mx.controls.Alert;
            import mx.events.AlertClickEvent;
            import flash.events.*;

            var file:FileReference;

            private function init(){
                Security.allowDomain("*");
                file = new FileReference();
                file.addEventListener(ProgressEventType.PROGRESS, onProgress);
                file.addEventListener(EventType.SELECT, onSelect);
            }

            private function upload(){
                file.browse();
            }
            
            private function onSelect(e:Event){
                Alert.show("上傳 " + file.name + " (共 "+Math.round(file.size)+" 位元組)?",
                           "確認上傳",
                           Alert.YES|Alert.NO,
                           null,
                           proceedWithUpload);
            }
            
            private function onProgress(e:ProgressEvent){
                lbProgress.text = " 已上傳 " + e.bytesLoaded 
                    + " 位元組,共 " + e.bytesTotal + " 位元組";
            }
            
            private function proceedWithUpload(e:AlertClickEvent){
                if (e.detail == Alert.YES){
                    file.upload("http://localhost/JZService/WebForm1.aspx");                    
                }
            }
        ]]>
    </mx:Script>
    
    <mx:Canvas width="100%" height="100%">
        <mx:VBox width="100%" horizontalAlign="center">
            <mx:Label id="lbProgress" text="上傳"/>
            <mx:Button label="上傳檔案" click="upload();"/>            
        </mx:VBox>
    </mx:Canvas>
</mx:Application>

服務端代碼:WebForm1.aspx

        private void Page_Load(object sender, EventArgs e) {
            // 在此處放置使用者代碼以初始化頁面
            HttpFileCollection uploadedFiles =  Request.Files;
            string Path = Server.MapPath("data");
            for(int i = 0 ; i < uploadedFiles.Count ; i++) {
                HttpPostedFile F = uploadedFiles[i];
                if(uploadedFiles[i] != null && F.ContentLength > 0) {   
                    string newName = F.FileName.Substring(F.FileName.LastIndexOf("\\") + 1);
                    F.SaveAs(Path + "/" + newName);
                }
            }

        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.