Friday, November 14, 2008

Generating dynamic XML file in FLEX(client side)

Well this is another area where i found very few resources .
may be because people would have thought it as so simple that it doesnt even require a help or support for anyone to build one....
But..i think i should answer the call now...
one may require at any point of time to generate a file or some sort of temporary data store, what more can it be other than XML????

the next important thing that matters is where it need to be generated, serverside or Client side?
if it is client side we need to do it in AS3 and is much much easier than on serverside...which is in AS1

what we do on in AS3 to generate in AS3 is:

import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import mx.controls.*;
import flash.filesystem.*;

[Bindable]
private var _file:File;
[Bindable]
private var _fileStream:FileStream;
[Bindable]
private var writeString:String="";


private function Radioalerts():void
{
if(rad1.selected) //rad1 is the id of the radio button i created

{writeString="&ltrad1>"+rad1.value+"\n";//simple concatenation of tag.}

if(rad2.selected) //rad2 is the id of the radio button i created

{writeString="&ltrad2>"+rad2.value+"\n";}

if(rad3.selected) //rad3 is the id of the radio button i created

{writeString= "&ltrad3>"+rad3.value+"\n"; }
}

private function writeXml():void
{

Radioalerts();

_file = File.documentsDirectory.resolvePath("XML Folder/New-xml.xml");

_fileStream = new FileStream();

writeString= writeString+writeString1+writeString2;

writeString = "&ltroot>"+writeString+"\n";

savefile();

}

private function savefile():void

{

_fileStream.open(_file,FileMode.WRITE);

_fileStream.writeUTFBytes(writeString);

_fileStream.close();
}

Well the basic idea is that you get the details that u want to be in the xml file of yours as a string and append the tags (simple concatenation) and write it to the file as u write a text file....thats it folks..!!!YOU HAVE GOT YOUR XML FILE in your documents directory .."My Documents/XML Folder/New-xml.xml"...

Thursday, November 13, 2008

Upload and download a file in flex/AIR (component)

During my early days of programming in flex i got a lot confused with the uplaod/download feature of flex, which in normal HTML and others are pretty easy. A sample upload file code in HTML would look like this:

< action="random.php" method="post" enctype="multipart/form-data">
Please choose a file: < type="file" name="uploaded">
&ltinput type="submit" value="Upload" >


which is hardly 3 lines of code.. but in flex we have much more formalities to be fullfilled before u can upload any file to the server...but you can use the same php/cf or any other server code for recieving the file uploaded...coz the flex is actually html+javascript and the effects done in flash.
Well...back to the topic.. For uploading a file in flex u need to import the "flash.net.*" class and flash.events class for the upload and other response events.The code goes like this:

&ltmx:Script>
&lt!--[CDATA[

import flash.events.*;
import flash.net.FileReference;
import flash.net.URLRequest;
import mx.controls.*;
import mx.controls.ProgressBar;
import mx.core.UIComponent;

//URL of the remote upload script or the server script which recieves the file.
private const UPLOAD_URL:String = "http://www.your-url.com/php_file_location";

/*var for accessing files ,you can also use FileReferenceList for multiple file upload scenario*/
private var fr:FileReference;

/* function that alows user to select a file through browse and each event listerts calls the respective function */

private function startUpload():void
{
fr = new FileReference();
fr.browse();
fr.addEventListener(Event.SELECT, selectHandler);
fr.addEventListener(Event.OPEN, openHandler);
fr.addEventListener(Event.COMPLETE, completeHandler);
}
/*Begin uploading the file specified in the UPLOAD_URL constant.*/
private function selectHandler(event:Event):void
{
textip1.text=event.target.name;
var request:URLRequest = new URLRequest();
request.url = UPLOAD_URL; fr.upload(request);
}
/*while the file is opened for upload you can add event here to alert file name or activate or disable any buttons etc...*/

private function openHandler(event:Event):void
{ /* any code snippet to show uploading */ }

/*event called once the upload has completed.*/

private function completeHandler(event:Event):void
{
Alert.show("UPLOADING COMPLETE");
cancel.enabled = false;
}

/*cancel the upload in progress and disable the cancel button.*/
public function cancelUpload():void
{
fr.cancel();
Alert.show( "UPLOAD CANCELLED");
cancel.enabled = false;
}
]]-->


The download file code is pretty much the same where instead of fr.upload();
you just need to type fr.download("filename.extension")
and the URL will be the filelocation.