1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="upLoad()" layout="vertical">
<mx:Script>
<![CDATA[
import mx.utils.*;
import mx.controls.Alert;
private var fileRef:FileReference;
private var fileSize:uint;
private var fileContents:ByteArray ;
//you need to initiate two scokets one for sending
//commands and second for sending data to FTP Server
//socket for sending commands to FTP
private var s:Socket
//responce from FTP
private var ftpResponce:String;
//socket for sending Data to FTP
private var dataChannelSocket:Socket;
//responce from FTP when sending Data to FTP
private var dataResponce:String;
//will hold the IP address of new socket created by FTP
private var dataChannelIP:String;
//will hold the Port number created by FTP
private var dataChannelPort:int;
private var user:String="user";//FTP usernae
private var pass:String="";//FTP Password
private function receiveReply(e:ProgressEvent):void{
ftpResponce = s.readUTFBytes(s.bytesAvailable)
var serverResponse:Number = Number(ftpResponce.substr(0, 3));
if(ftpResponce.indexOf('227')>-1){
//get the ip from the string response
var temp:Object = ftpResponce.substring(ftpResponce.indexOf("(")+1,ftpResponce.indexOf(")"));
var dataChannelSocket_temp:Object = temp.split(",");
dataChannelIP = dataChannelSocket_temp.slice(0,4).join(".");
dataChannelPort = parseInt(dataChannelSocket_temp[4])*256+int(dataChannelSocket_temp[5]);
//create new Data Socket based on dataChannelSocket and dataChannelSocket port
dataChannelSocket = new Socket(dataChannelIP,dataChannelPort);
dataChannelSocket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData);
}
//few FTP Responce Codes
switch(String(serverResponse)){
case "220" :
//FTP Server ready responce
break;
case "331" :
//User name okay, need password
break;
case "230":
//User logged in
break;
case "250" :
//CWD command successful
break;
case "227" :
//Entering Passive Mode (h1,h2,h3,h4,p1,p2).
break;
default:
}
//for more please
//http://http://www.altools.com/image/support/alftp/ALFTP_35_help/
//FTP_response_codes_rfc_959_messages.htm
traceData(ftpResponce);
}
private function receiveData(e:ProgressEvent):void{
dataResponce = dataChannelSocket.readUTFBytes(
dataChannelSocket.bytesAvailable);
traceData("dataChannelSocket_response>"+dataResponce);
}
private function showError(e:IOErrorEvent):void{
traceData("Error>"+e.text);
}
private function showSecError(e:SecurityErrorEvent):void{
traceData("SecurityError>"+e.text);
}
private function createRemoteFile(fileName:String):void{
if(fileName!=null && fileName !=""){
s.writeUTFBytes("STOR "+fileName+"\n");
s.flush();
}
}
private function sendData():void{
fileContents=fileRef.data as ByteArray;
fileSize=fileRef.size;
dataChannelSocket.writeBytes(fileContents,0,fileSize);
dataChannelSocket.flush();
}
//initialize when application load
private function upLoad():void {
fileRef = new FileReference();
//some eventlistener
fileRef.addEventListener(Event.SELECT, selectEvent);
fileRef.addEventListener(Event.OPEN, onFileOpen);
//this fucntion connect to the ftp server
connect();
//send the usernae and password
this.userName(user);
this.passWord(pass);
//if you want to change the directory for upload file
this.changeDirectory("/");//directory name
//enter into PASSV Mode
s.writeUTFBytes("PASV\n");
s.flush();
}
private function onFileOpen(event:Event):void {
}
private function traceData(event:Object):void {
var tmp:String = "================================\n";
ta.text +=event.toString()+ "\n" ;
ta.verticalScrollPosition += 20;
}
private function ioErrorEvent(event:IOErrorEvent):void{
Alert.show("IOError:" + event.text);
}
private function selectEvent(event:Event):void{
btn_upload.enabled = true;
filename.text = fileRef.name;
fileRef.load();
}
private function uploadFile():void {
createRemoteFile(fileRef.name);
sendData();
}
private function connect():void{
s = new Socket("127.0.0.1",2100);//Socket("ftp.anydomain.com",21);
s.addEventListener(ProgressEvent.SOCKET_DATA, receiveReply);
s.addEventListener(IOErrorEvent.IO_ERROR, showError);
s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, showSecError);
s.addEventListener(Event.CONNECT,onSocketConnect);
s.addEventListener(Event.CLOSE,onSocketClose);
s.addEventListener(Event.ACTIVATE,onSocketAtivate);
}
private function onSocketConnect(evt:Event):void {
//traceData("OnSocketConnect>"+evt.target.toString());
}
private function onSocketClose(evt:Event):void {
//traceData("onSocketClose>"+evt.target.toString());
}
private function onSocketAtivate(evt:Event):void {
//traceData("onSocketAtivate>"+evt.target.toString());
}
private function userName(str:String):void {
sendCommand("USER "+str);
}
private function passWord(str:String):void {
sendCommand("PASS "+str);
}
private function changeDirectory(str:String):void {
sendCommand("CWD "+str);
}
private function sendCommand(arg:String):void {
arg +="\n";
s.writeUTFBytes(arg);
s.flush();
}
]]>
</mx:Script>
<mx:Panel id="up" horizontalAlign="left" width="100%" height="100%">
<mx:Box width="100%" height="100%">
<mx:VBox >
<mx:Form width="449" height="284">
<mx:FormItem label="Selected File:">
<mx:Label id="filename"/>
</mx:FormItem>
<mx:FormItem >
<mx:Button width="80" label="Browse" click="fileRef.browse()" />
<mx:Button width="80" label="Upload" id="btn_upload" enabled="false"
click="uploadFile()" />
<mx:Button width="80" label="Cancel" id="btn_cancel" enabled="false"
click="fileRef.cancel()" />
</mx:FormItem>
<mx:HRule width="100%" tabEnabled="false"/>
<mx:FormItem label="Events:">
<mx:TextArea id="ta" width="260" height="98" />
</mx:FormItem>
</mx:Form>
</mx:VBox>
</mx:Box>
</mx:Panel>
</mx:Application> |
Partager