Tag Archives: transfer data to server

FileSaver Data Transfer

FileSaver Data Transfer

In my recent project, http://www.myflashlab.com/2010/01/27/contactform-class/ I was trying to do a lot of data transfer to and from flash to PHP (or any serverside script), I had to send attachments to php, I even had to encrypt the data before the transfer and that stuff. having to do all these things, I decided to write a class to take care of all these things for me.

so I wrote this fine class that I have named it FileSaver! :) This class is good for the following purposes:

  • you want to save a file from your flash app to your desktop.
  • you want to save a file from your flash app to your server.
  • you want to save a file to your server and send some variables along with it.
  • you want to send some variables to a server side script.
  • you can encrypt the data before sending them out to server script.

before I forget, I have used as3crypto for the encryption thing in my class.

you may download the class .as files here.

here is how you can work with it if you want to save a file on your server along with some variables to be send to the server side script.

import com.doitflash.tools.fileSaver.FileSaver;
import com.doitflash.tools.fileSaver.FileSaverConst;
import com.doitflash.events.FileSaverEvent;

// if we're saving to server
var _fileSaver:FileSaver = new FileSaver();
_fileSaver.method = FileSaverConst.SERVER;

// by default the encryption is turned off. if you are turning it on,
// make sure to put a key for your encryption which is not longer than 8 characters.
// to deal with encrypted data on your server side script, refer to http://code.google.com/p/as3crypto/
//_fileSaver.encrypt(true, "TESTTEST");
_fileSaver.encrypt(false);
_fileSaver.gateway = "phpProcessor.php";

// save all parameters that you want to send out in an object like below.
_fileSaver.vars = {var1:"value1",var2:"value2"};

// you may also wish to add a listener to receive the server script respond!
_fileSaver.addEventListener(FileSaverEvent.RESPOND, onRespond);

// call the save method and pass the file byteArray to it along with its name like below.
_fileSaver.save(_byte, "filename", ".gif");

function onRespond(e:FileSaverEvent):void
{
trace(e.paramURLVars) // paramURLVars is of type URLVariables
trace(e.paramOBJVars) // paramOBJVars is of type Object

// OR

trace(_fileSaver.theRespond);// theRespond is of type URLVariables
trace(_fileSaver.theRespondObject);// theRespondObject is of type Object
}

If you want to save some byteArray object on your local computer, try below:

import com.doitflash.tools.fileSaver.FileSaver;
import com.doitflash.tools.fileSaver.FileSaverConst;

var _fileSaver:FileSaver = new FileSaver();
_fileSaver.method = FileSaverConst.LOCAL;
_fileSaver.save(_byte, "filename", ".gif");

And finally if you don’t want to save any file but just send some variables to a server side script, try below:

import com.doitflash.tools.fileSaver.FileSaver;
import com.doitflash.tools.fileSaver.FileSaverConst;
import com.doitflash.events.FileSaverEvent;

var _fileSaver:FileSaver = new FileSaver();
_fileSaver.method = FileSaverConst.SERVER;

// by default the encryption is turned off. if you are turning it on,
// make sure to put a key for your encryption which is not longer than 8 characters.
// to deal with encrypted data on your server side script, refer to http://code.google.com/p/as3crypto/
//_fileSaver.encrypt(true, "TESTTEST");
_fileSaver.encrypt(false);
_fileSaver.gateway = "phpProcessor.php";

// save all parameters that you want to send out in an object like below.
_fileSaver.vars = {var1:"value1",var2:"value2"};

// you may also wish to add a listener to receive the server script respond!
_fileSaver.addEventListener(FileSaverEvent.RESPOND, onRespond);
_fileSaver.save();

function onRespond(e:FileSaverEvent):void
{
//trace(e.paramURLVars) // paramURLVars is of type URLVariables
//trace(e.paramOBJVars) // paramOBJVars is of type Object

// OR

trace(_fileSaver.theRespond);// theRespond is of type URLVariables
trace(_fileSaver.theRespondObject);// theRespondObject is of type Object
}



I am sure you will enjoy this small but useful class :) it will help you get rid of all those URLLoader and URLVariables and that stuff! it will do them all with an easy interface.

I hope you like it,
Hadi

Leave a comment
Contact UsContact Us
Contact MyFlashLab Team?

Feel free to drop us an email if you have any question regarding the TextArea class or the tag modules.

* required
Send Message