Flex

Flex教學-WEB瀏覽本機檔案經由Ftp上傳檔案

梅干2009/08/17
梅問題-flex教學-web瀏覽本機檔案再經ftp上傳檔案
  WEB中要上傳檔案都是得透過http的方式,將檔案上傳到遠端去,當今天檔案太大時,則會出現時間逾時的問題, 因此梅干在想若可以從web中,將檔案透過ftp的方式來上傳,不就可解決此問題,同時ftp又有續傳的功能,於是上谷哥查了一下,Flash player10已支援ftp上傳的功能,因此梅干就用Flex來進行開發, 之前可登入也可傳,但卻只行傳送文字檔,搞了老半天才知道,原來是上傳時還得告知主機,目前所上傳的編碼類型為何,目前終於成功的可正常的上下傳任何檔案了~在這過程中也要特別感謝鄧教授山羊這幾位好朋友的大力相助。

Step1
建立一個新的專案,再將Flex CompilerFlash Player版本設為10.0.0
梅問題-flex教學-web瀏覽本機檔案再經ftp上傳檔案
Step2
FlexFtpUload原始碼如下:
import mx.utils.*; import mx.controls.Alert; import flash.net.FileReference; import flash.net.*; import flash.utils.*; //檔案大小與檔案傳輸類型 private var fileSize:uint; private var fileContents:ByteArray; //連線用Socket private var ftpSocket:Socket; private var ftpResponce:String; //上傳檔案用Socket private var upLoadSocket:Socket; private var upLoadResponce:String; //取得使用者的IP與Port private var ClientIP:String; private var ClientPort:int; //連線到FTP資訊 private var server:String="";//FTP 主機位置ex.ftp.abc.com private var user:String="";//FTP 帳號 private var pass:String="";//FTP 密碼 private var dir:String="";//FTP 上傳資料夾 //Ftp連線 private function connect():void{ this.connbtn.enabled=false; ftpSocket = new Socket(server,21); sendCommand("USER "+this.user); sendCommand("PASS "+this.pass); sendCommand("CWD "+dir); ftpSocket.addEventListener(ProgressEvent.SOCKET_DATA, SocketData); ftpSocket.addEventListener(IOErrorEvent.IO_ERROR, IOError); ftpSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, SecError); ftpSocket.addEventListener(Event.CONNECT, SocketConn); ftpSocket.addEventListener(Event.CLOSE, SocketClose); ftpSocket.addEventListener(Event.ACTIVATE, SocketAtivate); } private function SocketData(e:ProgressEvent):void{ ftpResponce = ftpSocket.readUTFBytes(ftpSocket.bytesAvailable) var serverResponse:Number = Number(ftpResponce.substr(0, 3)); if(ftpResponce.indexOf('227')>-1){ //取得使用者的ip位置 var temp:Object = ftpResponce.substring(ftpResponce.indexOf("(")+1 ,ftpResponce.indexOf(")")); var upLoadSocket_temp:Object = temp.split(","); ClientIP = upLoadSocket_temp.slice(0,4).join("."); ClientPort = parseInt(upLoadSocket_temp[4])*256+ int(upLoadSocket_temp[5]); //建立一個上傳的PORT upLoadSocket = new Socket(ClientIP,ClientPort); upLoadSocket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData); } switch(String(serverResponse)){ case "220": //FTP連線就續 break; case "331"://帳號ok,密碼錯誤 break; case "230"://登入成功 //指定下載文件的類型,I是二進位文件,A是字元文件 sendCommand("TYPE A");//設定TYPE為ASCII sendCommand("TYPE I");//設定上傳的編碼為8-bit binary sendCommand("PASV");//passive模式 break; case "250" ://資料夾切換成功 break; case "227" : //Entering Passive Mode (h1,h2,h3,h4,p1,p2). break; default: } traceData(ftpResponce); } private function IOError(e:IOErrorEvent):void{ traceData("—>Error:"+e.text); } private function SecError(e:SecurityErrorEvent):void{ traceData("–>SecurityError:"+e.text); } private function SocketConn(evt:Event):void { traceData("–>OnSocketConnect:"+evt.target.toString()); } private function SocketAtivate(evt:Event):void { traceData("–>onSocketAtivate"); sendCommand("PWD"); ftpSocket.flush(); } private function SocketClose(evt:Event):void { traceData("–>onSocketClose"); sendCommand("REST 0"); sendCommand("PWD"); ftpSocket.flush(); } //ProgressEvent.SOCKET_DATA private function receiveData(e:ProgressEvent):void{ upLoadResponce = upLoadSocket.readUTFBytes(upLoadSocket.bytesAvailable); traceData("upLoadSocket_response—>"+upLoadResponce); } //瀏覽檔案 private function selectEvent(event:Event):void{ upLoadbtn.enabled = true; this.filename.text = fileRef.name; fileRef.load(); } //檔案上傳 private function uploadFile():void { createRemoteFile(fileRef.name);//下上傳指令 sendData();//送出檔案 } private function createRemoteFile(fileName:String):void{ if(fileName!=null && fileName !=""){ sendCommand("STOR "+fileName);//上傳指令 ftpSocket.flush(); } } //檔案轉二進位傳送 private function sendData():void{ fileContents=fileRef.data as ByteArray; fileSize=fileRef.size; upLoadSocket.writeBytes(fileContents,0,fileSize); upLoadSocket.flush(); } //處理Ftp指令 private function sendCommand(arg:String):void { arg +="\n"; ftpSocket.writeUTFBytes(arg); ftpSocket.flush(); } //顯示資訊 private function traceData(event:Object):void { var tmp:String = "================================\n"; infotxt.text +=event.toString()+ "\n" ; infotxt.verticalScrollPosition += 20; }
[範例下載]
UAG軍規iPhone防摔殼