flex+php實現檔案的上傳

來源:互聯網
上載者:User

網上找了一個用fileRefrenceList作為檔案的選擇,貌似用這種方法不能限定上傳檔案的類型,不過經過測試,確實能啟動並執行。

為方便起見,把它封裝成了一個類,並加上了注釋,便於大家和自己以後使用。

UploadImage.as:

package exchage
{
 import flash.events.Event;
 import flash.net.FileReference;
 import flash.net.FileReferenceList;
 import flash.net.URLRequest;
 import mx.controls.Alert;
 
 public class UploadImage
 {

      private var urlRequest:URLRequest;
         private var fileReferenceList:FileReferenceList;
         private var serverSideScript:String = http://www.路徑/uploadImage.php;  //處理上傳的指令檔路徑
         
         [Bindable]
         public var statusText:String;                                                                    //記錄上傳檔案狀態
   
            public function init():void
            {
           urlRequest = new URLRequest(serverSideScript);                                      //串連上
             fileReferenceList = new FileReferenceList();
               fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler);    //設定檔案選擇監聽
            }
    
       public  function uploadFile():void
       {
             init();
             fileReferenceList.browse();                                                                    //開啟瀏覽對話方塊
       }
    
       private function fileSelectedHandler(event:Event):void                                    //檔案選擇監聽響應函數
       {
        var fileReference:FileReference;
          var fileReferenceList:FileReferenceList = FileReferenceList(event.target);
          var fileList:Array = fileReferenceList.fileList;

          fileReference = FileReference(fileList[0]);
           
          fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);  //設定上傳過程結束監聽
          fileReference.upload(urlRequest);
          
          statusText = "上傳中...";

       }
    
       private function uploadCompleteHandler(event:Event):void                               //檔案上傳完畢監聽相應函數
       {
          statusText = "上傳成功的檔案: " + event.target.name;
       }

 }
}

 

uploadImage.php

<?php
$uploaddir = 'uploadImages/';
$uploadfile = $uploaddir . basename($_FILES['Filedata']['name']);
$temploadfile = $_FILES['Filedata']['tmp_name'];
move_uploaded_file($temploadfile , $uploadfile);
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.