Feb 21 2010

FileSaver Data Transfer

Category: AS3admin @ 7:22 am

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.

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
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:

1
2
3
4
5
6
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:

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
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

Tags: , , , , , , , , , ,

2 Responses to “FileSaver Data Transfer”

  1. Amine HSISSI says:

    Hi,
    I start using FileSaver Data Transfer class in my project, however, I’have some issues with the PHP code.
    Could you please give a sample how do you handle file data with PHP Code ?

    Thanks

  2. Roberto Leite says:

    Hi!
    Good job.
    I’m (desperatelly) looking for a component or class like yours FileSaver.
    Your FileSaver seems to be efective, however it worked only on AIR and not on the server side, to save an image file to my hard drive.
    It could not open the file/save dialog box in my desktop(win 7) to finish the saving process.

    Can you give me some clues to solve this issue ?
    Thanks a lot !

    Roberto Leite
    Brazil

Leave a Reply

*