Quantcast
Viewing all articles
Browse latest Browse all 2010

Air for android - Can't upload to ftp

I'm trying to upload a simple text file to ftp, the code works on the pc.

On android the app connects succesfully to the ftp but when I try to upload the file I get "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 to ftp server!"

//

var ftp_host:String = "*****";     //FTP settings are correct in the original code...

var ftp_port:Number = 21;

var ftp_username:String = "*****";

var ftp_password:String = "*****";

//

//in this demo we will be manually entering the remote servers folder from which upload or download should happen

var remoteFolderStr:String;

var localFolderStr:String;

var remoteFile:String;

//Socket instance which will be used to connect to 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 which will be used to connect to receive data sent by 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 the client sends to ftp server the server returns message with return codes

function onReturnData(evt:ProgressEvent)

{

    var d = s.readUTFBytes(s.bytesAvailable);

    trace(d);

    //check here for complete list of return codes and their meaning

    //- http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

 

    // the return message will have a 3 digit return code followed by a space and related message

    // if the 3 digit return code is followed by "-" the it will be a multiline message

    //-wait until the line with 3 digit code followed by space is delivered

    if (d.indexOf("220 ") > -1)

    {

        msg.text="Logging in to ftp server!"

        //connected to ftp server send user name to server

        s.writeUTFBytes("USER "+ftp_username+"\n");

        s.flush();

    }

    if (d.indexOf("331 ") > -1)

    {

        //Username accepted now send password to 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 and retrive a list of files from a directory

        //first enetr passive mode

        //s.writeUTFBytes("PASV \n");

        //s.flush();

        

    }

    var a = d.indexOf('227');

    if (a > -1)

    {

        //Entering passive mode message will be returned along with it details of ip and port address will be returned

        //-we have to connect to that address to receive the data

        //format of the message will be: 227 Entering Passive Mode (209,190,85,253,148,206)

        //the data inside brackets is the ip and port address, first four numbers represent IP and last 2 PORT

        //the port value have to be calculated by multiplying the 5th number with 256 and adding the 6th number to it

        //here in this example IP is 209.190.85.253 , PORT is (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 from server

    }

    //Response code 150 will be sent by server whenever a data connection is established after we send 'PASV' command

    if (d.indexOf("150 ") > -1)

    {

        if (process == 'upload')

        {

            //Once data connection is established we can start sending the data to the server

            startSendingData();

        }

    }

}

function onConnectHandler(evt:Event)

{

    msg.text="CONNECTED TO FTP SERVER!"

    trace("CONNECTED TO FTP SERVER");

    //Client has connected to ftp server

}

 

function onPasvConn(evt:Event):void

{

    trace("CONNECTED TO DATA PORT");

    

    //s.writeUTFBytes("LIST /your/folder/path\n");

 

    if (process=='upload')

    {

        //To Upload a file send following command

        //STOR is the command followed by a space and path wher to store the file in remote server

        //-with the name of the file to be saved as..you can provide any name with extension

        s.writeUTFBytes("STOR /test.txt\n");

    }

    s.flush();

}

function onServData(evt:ProgressEvent):void

{

    //DATA RECEIVED FROM SERVER THRO DATA PORT

}

function onIOERR(evt:IOErrorEvent)

{

    trace(evt.errorID+":"+evt.text);

}

//

var process:String = "";// variable to store what action is being performed

var writeToStream:Boolean;//not used here

var localFile:File;//local file which it to be uploaded

var localFolder:File;// local folder to which the downloaded file to be stored

var interval:Number;

var bufferSize:int;

//

 

BTN_Upload.addEventListener(MouseEvent.CLICK,initUpload);

function initUpload(evt:MouseEvent=null):void

{

    //called when upload event is triggered

    startUploadProcess();

}

 

function startUploadProcess():void

{

    process = "upload";

    //You need to pass this command 'TYPE I' to set data transfer mode as binary

    s.writeUTFBytes("TYPE I\n");

    s.writeUTFBytes("PASV \n");

    s.flush();

}

 

function startSendingData():void

{

    var textToUpload:String = "This is a test";

 

    bufferSize = textToUpload.length;

   

    var ba:ByteArray = new ByteArray();

   

    //Store the info you want to upload in ByteArray HERE

    ba.writeMultiByte(textToUpload, "iso-8859-1");

   

    r.writeBytes(ba, 0, ba.bytesAvailable);

    r.flush();

}


Viewing all articles
Browse latest Browse all 2010

Trending Articles