Problem Uploading Large File Using FTP and Socket in AS3
Copy link to clipboard
Copied
Hi all,
i made ftp upload using socket in as3 , and it works good with small files , but if i upload file larger than 200MB my flash gets hanged and stopped. can anybody help me on how can i make large file uploads happen without any stop?
Here i paste my code for reference
import flash.net.Socket;
import fl.controls.Button;
import fl.controls.ProgressBar;
import fl.controls.ProgressBarMode;
import flash.events.Event;
import Math;
//pb is placed on the stage
pb.visible=false;
pb.setProgress(0,0);
cancel_btn.addEventListener(MouseEvent.CLICK, CancalUpload);
cancel_btn.visible =false;
cancel_btn.buttonMode = true;
upload_btn.addEventListener(MouseEvent.CLICK, getFile);
var AllowedFiles:String = getFlashVars().filetypes == undefined ? "*.mp3;*.wmv;*.wav;*.mp4" : getFlashVars().filetypes;
var AllowedSize:int = getFlashVars().filesize == undefined ? 524288000 : getFlashVars().filetypes;
var Process:String="";
// File Reference + event Handlers
var imageFilter:FileFilter = new FileFilter("Videos", AllowedFiles);
var myFileReference = new FileReference();
myFileReference.addEventListener(Event.SELECT, onSelect);
myFileReference.addEventListener(Event.OPEN,OnOpen)
myFileReference.addEventListener(Event.COMPLETE, onComplete);
// extra vars
var ftp_response:String;
var ftp_response_code:Number;
var data_response:String;
var data_channel_ip:String;
var data_channel_port:int;
var data_channel:Socket;
var i_bytes_send:int;
var i_block_count:int;
var ftp_host:String = "ipaddress";
var ftp_port:Number = 21;//usually 21
var ftp_username:String = "myusername";
var ftp_password:String = "mypassword";
// not sure if I need this!
//Security.loadPolicyFile('xmlsocket:/ipaddress/crossdomain.xml');
Security.allowDomain("*");
// Command Socket + event Handlers
var s:Socket = new Socket();
s.addEventListener(ProgressEvent.SOCKET_DATA, receiveReply);
s.addEventListener(IOErrorEvent.IO_ERROR, showError);
s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, showSecError);
function OnOpen(e:Event):void
{
pb.visible=true;
cancel_btn.visible =true;
//txtfile.text = myFileReference.name;
}
// Start with file selector
function getFile(e:MouseEvent):void
{
myFileReference.browse([imageFilter]);
}
function CancalUpload(e:MouseEvent):void
{
if(s.connected)
{
trace("close connection");
s.close();
}
Process ="delete";
s.connect(ftp_host,ftp_port);
}
function DeleteFile()
{
trace("delete file called");
pb.visible= false;
cancel_btn.visible= false;
s.writeUTFBytes("DELE " + myFileReference.name+"");
sendSocket(s);
}
//Event Handlers
function receiveReply(e:ProgressEvent):void
{
try
{
ftp_response = s.readUTFBytes(s.bytesAvailable);
ftp_response_code = Number(ftp_response.substr(0, 3));
trace(ftp_response);
if (ftp_response_code == 220)
{
//connected to ftp server send user name to server
s.writeUTFBytes("USER "+ftp_username+"\n");
s.flush();
}
if (ftp_response_code == 331)
{
//Username accepted now send password to server
s.writeUTFBytes("PASS "+ftp_password+"\n");
s.flush();
}
if (ftp_response_code == 230)
{
s.writeUTFBytes("CWD /\n");
s.flush();
}
if (ftp_response_code == 250)
{
if(ftp_response.indexOf("CWD command successful") > 0)
{
s.writeUTFBytes("TYPE I");
sendSocket(s);
}
}
if (ftp_response_code == 200)
{
s.writeUTFBytes("PASV");
sendSocket(s);
}
if (ftp_response_code == 227)
{
if(Process =="upload")
{
Process="";
//get the ip from the string response
var temp = ftp_response.substring(ftp_response.indexOf("(")+1,ftp_response.indexOf(")"));
var data_channel_temp = temp.split(",");
data_channel_ip = data_channel_temp.slice(0,4).join(".");
//calculate the port number from the last two digits - if this makes no sense, google FTP PROTOCOL - there are loads of guides that explain the server responses.
data_channel_port = parseInt(data_channel_temp[4])*256+int(data_channel_temp[5]);
trace("data_channel_port : "+data_channel_port);
//use the new IP to open a second socket - this will transmit the data. Your first socket will transmit any commands you issue and this new socket will transmit the data
data_channel = new Socket(data_channel_ip,data_channel_port);
data_channel.addEventListener(ProgressEvent.SOCKET_DATA, receiveData);
data_channel.addEventListener(IOErrorEvent.IO_ERROR, showError);
data_channel.addEventListener(SecurityErrorEvent.SECURITY_ERROR, showSecError);
data_channel.addEventListener(Event.CONNECT, dataConnect);
}
else if(Process =="delete")
{
Process="";
DeleteFile();
}
}
if (ftp_response_code == 150 || ftp_response_code == 125)
{
upload();
}
if (ftp_response.indexOf('226') > -1)
{
s.close();
}
}
catch (errObject:Error) {
trace("\nCatch The message is: " + errObject.message);
SendJsFunc(errObject.message,"error");
}
}
var _interval:int;
function upload():void {
_interval = setInterval(sendData, 0);
}
function sendData():void {
try
{
var _bufferRate:int = System.freeMemory / 2;
if (myFileReference.data.bytesAvailable < _bufferRate)
_bufferRate = myFileReference.data.bytesAvailable;
i_bytes_send= i_bytes_send + _bufferRate
trace("Bytes Send :"+i_bytes_send);
pb.setProgress(i_bytes_send,myFileReference.size);
if (myFileReference.data.bytesAvailable <= 0){
clearInterval(_interval);
data_channel.close();
SendJsFunc("Upload Completed Successfully","success");
return;
}
var ba:ByteArray = new ByteArray();
myFileReference.data.readBytes(ba, 0, _bufferRate);
data_channel.writeBytes(ba, 0, ba.bytesAvailable);
data_channel.flush();
}
catch (errObject:Error)
{
trace("\nCatch The message is: " + errObject.message);
SendJsFunc(errObject.message,"error");
}
}
function receiveData(e:ProgressEvent):void
{
try
{
data_response = data_channel.readUTFBytes(data_channel.bytesAvailable);
trace('DATA: ' + data_response);
}
catch (errObject:Error)
{
trace("\nCatch The message is: " + errObject.message);
SendJsFunc(errObject.message,"error");
}
}
function showError(e:IOErrorEvent):void
{
trace('ERROR :' + e.text);
SendJsFunc(e.text,"error");
}
function showSecError(e:SecurityErrorEvent):void
{
trace('SECERROR :' + e.text);
SendJsFunc(e.text,"error");
}
function onSelect(e:Event):void
{
myFileReference.load();
}
function sendSocket(s:Socket):void
{
s.writeByte( 13 );
s.writeByte( 10 );
s.flush();
}
function onComplete(e:Event):void
{
Process ="upload";
trace(myFileReference.size+","+AllowedSize);
if(myFileReference.size <= AllowedSize)
{
s.connect(ftp_host, ftp_port);
}
else
{
var SizeinMB:int = AllowedSize /(1024 * 1024);
trace("File size can not exceed "+ SizeinMB +"MB");
SendJsFunc("File size can not exceed "+ SizeinMB +"MB","error");
}
}
function dataConnect(e:Event):void
{
try
{
SaveFile();
}
catch (errObject:Error) {
SendJsFunc(errObject.message,"error");
trace("\nCatch The message is: " + errObject.message);
}
}
function SaveFile()
{
s.writeUTFBytes("STOR " + myFileReference.name);
sendSocket(s);
}
import flash.external.ExternalInterface;
function SendJsFunc(msg,type)
{
if(type == "error")
{
ExternalInterface.call("ShowError", msg);
}
if(type == "success")
{
ExternalInterface.call("ProcessComplete", msg);
}
}
function getFlashVars():Object {
return Object( LoaderInfo( this.loaderInfo ).parameters );
}
Its really urgent,
Thanks Meghana
Copy link to clipboard
Copied
Hello Meghana...I was wondering if you found a solution for this...I was wondering if you found a solution for this because I'm struggling with this myself....although I'm using the "TYPE L 8" transfer format/type ....
Thanks
Tumiso

