Air for android - Can't upload to ftp
i'm trying upload simple text file ftp, code works on pc.
on android app connects succesfully ftp when try upload file "2031:error #2031: socket error."
any ideas?
the code (from http://suzhiyam.wordpress.com/2011/04/) is:
import flash.events.progressevent;
import flash.events.event;
import flash.net.socket;
import flash.events.ioerrorevent;
import flash.errors.ioerror;
import flash.filesystem.filestream;
import flash.filesystem.file;
import flash.utils.bytearray;
import flash.events.mouseevent;
msg.text="wait! connecting ftp server!"
//
var ftp_host:string = "*****"; //ftp settings correct in original code...
var ftp_port:number = 21;
var ftp_username:string = "*****";
var ftp_password:string = "*****";
//
//in demo manually entering remote servers folder upload or download should happen
var remotefolderstr:string;
var localfolderstr:string;
var remotefile:string;
//socket instance used connect ftp server
var s = new socket(ftp_host,ftp_port);
s.addeventlistener(ioerrorevent.io_error,onioerr);
s.addeventlistener(progressevent.socket_data, onreturndata);
s.addeventlistener(event.connect, onconnecthandler);
//
//socket instance used connect receive data sent ftp server
var r:socket = new socket();
r.addeventlistener(progressevent.socket_data, onservdata);
r.addeventlistener(event.connect, onpasvconn);
r.addeventlistener(ioerrorevent.io_error,onioerr);
//for every command client sends ftp server server returns message return codes
function onreturndata(evt:progressevent)
{
var d = s.readutfbytes(s.bytesavailable);
trace(d);
//check here complete list of return codes , meaning
//- http://en.wikipedia.org/wiki/list_of_ftp_server_return_codes
// return message have 3 digit return code followed space , related message
// if 3 digit return code followed "-" multiline message
//-wait until line 3 digit code followed space delivered
if (d.indexof("220 ") > -1)
{
msg.text="logging in ftp server!"
//connected ftp server send user name server
s.writeutfbytes("user "+ftp_username+"\n");
s.flush();
}
if (d.indexof("331 ") > -1)
{
//username accepted send password server
s.writeutfbytes("pass "+ftp_password+"\n");
s.flush();
}
if (d.indexof("230") > -1 && d.indexof("ok.") > -1)
{
msg.text="log in successful!"
//password accepted - lets enetr passive mode , retrive list of files directory
//first enetr passive mode
//s.writeutfbytes("pasv \n");
//s.flush();
}
var = d.indexof('227');
if (a > -1)
{
//entering passive mode message returned along details of ip , port address returned
//-we have connect address receive data
//format of message be: 227 entering passive mode (209,190,85,253,148,206)
//the data inside brackets ip , port address, first 4 numbers represent ip , last 2 port
//the port value have calculated multiplying 5th number 256 , adding 6th number it
//here in example ip 209.190.85.253 , port (148*256)+206 = 38094
var st = d.indexof("(",a);
var en = d.indexof(")",a);
var str;
str = d.substring(st + 1,en);
var a2 = str.split(",");
var p1 = a2.pop();
var p2 = a2.pop();
var ip:string = a2.join(".");
var port:int=(p2*256)+(p1*1);
r.connect(ip, port);
}
if (d.indexof("226 ") > -1)
{
//data transfer completed
//s.writeutfbytes("quit \n");
//s.flush();
if (process=='download')
{
msg.text="download_complete"
}
if (process=='upload')
{
msg.text="upload_complete"
}
dispatchevent(new event("datareceived"))
}
if (d.indexof("221 ") > -1)
{
//logged out server
}
//response code 150 sent server whenever data connection established after send 'pasv' command
if (d.indexof("150 ") > -1)
{
if (process == 'upload')
{
//once data connection established can start sending data server
startsendingdata();
}
}
}
function onconnecthandler(evt:event)
{
msg.text="connected ftp server!"
trace("connected ftp server");
//client has connected ftp server
}
function onpasvconn(evt:event):void
{
trace("connected data port");
//s.writeutfbytes("list /your/folder/path\n");
if (process=='upload')
{
//to upload file send following command
//stor command followed space , path wher store file in remote server
//-with name of file saved as..you can provide name extension
s.writeutfbytes("stor /test.txt\n");
}
s.flush();
}
function onservdata(evt:progressevent):void
{
//data received server thro data port
}
function onioerr(evt:ioerrorevent)
{
trace(evt.errorid+":"+evt.text);
}
//
var process:string = "";// variable store action being performed
var writetostream:boolean;//not used here
var localfile:file;//local file uploaded
var localfolder:file;// local folder downloaded file stored
var interval:number;
var buffersize:int;
//
btn_upload.addeventlistener(mouseevent.click,initupload);
function initupload(evt:mouseevent=null):void
{
//called when upload event triggered
startuploadprocess();
}
function startuploadprocess():void
{
process = "upload";
//you need pass command 'type i' set data transfer mode binary
s.writeutfbytes("type i\n");
s.writeutfbytes("pasv \n");
s.flush();
}
function startsendingdata():void
{
var texttoupload:string = "this test";
buffersize = texttoupload.length;
var ba:bytearray = new bytearray();
//store info want upload in bytearray here
ba.writemultibyte(texttoupload, "iso-8859-1");
r.writebytes(ba, 0, ba.bytesavailable);
r.flush();
}
More discussions in AIR Development
adobe
Comments
Post a Comment