Save File in AS3

Opens a dialog box that lets the user save a file to the local filesystem. Although Flash Player has no restriction on the size of files you can upload, download, load or save, the player officially supports sizes of up to 100 MB.

The save() method first opens a dialog box that asks the user to enter a filename and select a location on the local computer to save the file. once Selected and confirms the save operation (for example, by clicking Save), the save process begins. Listeners receive events to indicate the progress, success, or failure of the save operation.

FileReference save method has only to be invoked upon user interaction, for example by a mouse click or button press.


<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” >
<mx:Script>
<![CDATA[
private var file:FileReference = new FileReference();
private function onCreate(e:Event):void
{
var Txt:String=”Hello World”
file.save(Txt,”Sample.txt”); // Initiates the save event and opens up a dialog box for user to enter filename and select a location. Once confirmed, it get saved
} ]]>
</mx:Script>
<mx:Button x=”133″ y=”209″ label=”Create File” click=”onCreate(event)”/>
</mx:Application>

Store Object in an Array

Storing Object into an Array

First Method

var Ar:Array=new Array() //Ar is an Array and obj is an object which have two identifiers fname and lname.
var Obj:Object={fname:”Steve”,lname:”Chan”} //Define obj with two identifiers(variables) fname and lname
Ar[0]=Obj // Store obj in Ar at 0 index
trace(Ar[0].fname) //Returns Steve

Second Method
var Ar:Array=new Array({fname:”Steve”,lname:”Chan”}) //Store object in Ar at 0 index
trace(Ar[0].fname) //Returns Steve

Third Method

var Ar:Array=new Array() //Ar is an Array
var Obj:Object={fname:”Steve”,lname:”Chan”} //Define obj with two identifiers(variables) fname and lname
Ar.push(obj) // Pushing (Storing Obj in ar) starts from 0 index
trace(Ar[0].fname) //Returns Steve

Fourth Method

var Ar:Array=new Array() //Ar is an Array
Ar[0].fname=”Steve” //creating an identifier named “fname” in an Array at 0 index.
Ar[0].lname=”Chan” //creating an identifier named “lname” in an array at 0 index
trace(Ar[0].fname) // return Steve

Object Declaration

Different Way of Declaring and storing Objects

First Method

var obj:Object=new Object()
obj={fname:”Steve”, lname:”chan”}
trace(obj.fname) //Returns Steve

Second Method

var obj:Object={fname:”Steve”, lname:”chan”}
trace(obj.fname) //Returns Steve

Third Method

var obj:Object=new Object()
obj.fname=”Steve”
obj.lname=”chan”
trace(obj.fname) //Returns Steve

Fourth Method

var obj:Object=new Object()
obj[“fname”]=”Steve”
obj[“lname”]=”chan”
trace(obj[“fname”]) //Returns Steve
trace(obj.fname) //Returns Steve

Array Declaration

The Array class lets you access and manipulate arrays. Array indices are zero-based, which means that the first element in the array is [0], the second element is [1], and so on.

Different Way of Declaring and storing Arrays

First Method

var Ar:Array=new Array()
Ar[0]=”Orange”
Ar[1]=”Apple”
trace(Ar) // Returns Orange, Apple
trace(Ar[0]) //Returns Orange

Second Method

var Ar:Array=new Array(“Orange”,”Apple”)
trace(Ar) // Returns Orange, Apple
trace(Ar[0]) //Returns Orange

Third Method

var Ar:Array=new Array()
Ar.push(“Orange”)
Ar.push(“Apple”)
trace(Ar) //Orange, Apple

Fourth Method

var Ar:Array=new Array()
Ar.push(“Orange”,”Apple”)
trace(Ar) // Orange, Apple

Fifth Method

var Ar=[]
Ar.push(“Orange”,”Apple”)
trace(Ar) // Orange, Apple

Get Unique values from Array

This can be achieved (in AS2 and AS3) in a much simpler manner, using the following function or prototype:

Array.prototype.countValues = function ()
{
var z, x = this.length, c = false, a = [], d = [];
while (x–) { z = 0;  while (z < x){ if (this[x] == this[z]){ c = true; d.push (this[x]); break; }
z++;} if (!c) a.push ([this[x], 1]); else c = false; } y = a.length;while (y–) { q = 0; while (q < d.length) { if (a[y][0] == d[q]) a[y][1]++; q++; }}
return a;
};

For an example i have an array [“Orange”,”Apple”,”Apple”,”Orange”,”Pappaya”],
Would like to get the count of each values [Orange => 2, Apple => 2, Pappaya
=>1]

Once the following function is invoked for this array, it removes the all the
duplicates and generate a new Array with Unique values and it counts.

Usage

var SourceArray=[“Orange”,”Apple”,”Apple”,”Orange”,”Pappaya”]
var NewArray=SourceArray.countValues()
trace(NewArray)//Returns Pappaya,1,Apple,2,Orange,2

Access SharePoint list in Flex

Simply put together the link as below.

http://[Server]/_vti_bin/owssvr.dll?Cmd=Display&List={GUID}&XMLDATA=TRUE

How to get GUID for List  

1. Site Actions -> Site Settings

2. Click on Site Libraries and lists

3.click on the list which you would like to access as XML.

4. Get the List ID from the Redirected URL

5.http://[Server]/_vti_bin/owssvr.dll?Cmd=Display&List={80D18622-447F-4900-90A5-E424EDB95DCD}&XMLDATA=TRUE

6. XML Output

How to read SharePoint List from Flex 

<?xml version=”1.0″ encoding=”utf-8″?><mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
creationComplete=”onCreate(event)”><mx:Script><![CDATA[
import mx.controls.Alert;
private function onCreate(e:Event):void
{
var XMLLoader:URLLoader=new URLLoader(new URLRequest(“http://[localHost]/_vti_bin/owssvr.dll?Cmd=Display&List={80D18622-447F-4900-90A5-E424EDB95DCD}&XMLDATA=TRUE”))
XMLLoader.addEventListener(Event.COMPLETE,openXMLValues)
function openXMLValues(e:Event):void
{
var XMLL:XMLList=new XMLList(e.target.data)
var rsNS:Namespace = XMLL.namespace(“rs”);// Create Namespace inorder to access the XML node <rs:>
var zNS:Namespace = XMLL.namespace(“z”);// Create Namespace inorder to access the XML node <z:
var sNS:Namespace = XMLL.namespace(“s”); // Create Namespace inorder to access the XML node <s:
var FinalXML:XMLList=new XMLList(XMLL.rsNS::data.zNS::row) // access the node <rs:row>
Alert.show(FinalXML.@ows_LinkTitle)
}
}]]>
</mx:Script>
</mx:Application>

URLLoader  

The URLLoader class downloads data from a URL as text, binary data, or
URL-encoded variables. It is useful for downloading text files, XML, or other
information to be used in a dynamic, data-driven application.

var XMLLoader:URLLoader=new URLLoader(new URLRequest(“http://[localHost]/_vti_bin/owssvr.dll?Cmd=Display&List={80D18622-447F-4900-90A5-E424EDB95DCD}&XMLDATA=TRUE“))

XMLLoader.addEventListener(Event.COMPLETE,openXMLValues)

XMLList  

The XMLList class contains methods for working with one or more XML elements. An
XMLList object can represent one or more XML objects or elements (including
multiple nodes or attributes), so you can call methods on the elements as a
group or on the individual elements in the collection.

var XMLL:XMLList=new XMLList(e.target.data)

XMLL will have the entire XML elements including all attributes and nodes.

RecordSet XML vs Normal XML  

RecordSet XML

<xml xmlns:s=’uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882′ xmlns:dt=’uuid:C2F41010-65B3-11d1-A29F-00AA00C14882′xmlns:rs=’urn:schemas-microsoft-com:rowset’ xmlns:z=’#RowsetSchema’>
<s:Schema id=’RowsetSchema’>
<s:ElementType name=’row’ content=’eltOnly’ rs:CommandTimeout=’30’>
<s:AttributeType name=’ID’ rs:number=’1′>
<s:datatype dt:type=’int’ dt:maxLength=’4′ rs:precision=’10’ rs:fixedlength=’true’ rs:maybenull=’false’/>
</s:AttributeType>
<s:AttributeType name=’c1′ rs:name=’Status Code’ rs:number=’2′rs:writeunknown=’true’>
<s:datatype dt:type=’string’ dt:maxLength=’20’ rs:fixedlength=’true’ rs:maybenull=’false’/>
</s:AttributeType>
<s:extends type=’rs:rowbase’/>
</s:ElementType>
</s:Schema>
<rs:data><z:row ID=’1′ c1=’Add ‘/>
<z:row ID=’2′ c1=’Drop ‘/>
<z:row ID=’3′ c1=’Modify Date ‘/>
<z:row ID=’4′ c1=’Complete ‘/>
<z:row ID=’5′ c1=’Modify Parameters ‘/>
</rs:data>
</xml>

In order to access <z:row> recordset, you need to create Namespace for z: , rs:
, s: ,etc..,

var XMLL:XMLList=new XMLList(e.target.data)
var rsNS:Namespace = XMLL.namespace(“rs”);// Create Namespace inorder to access
the XML node <rs:>
var zNS:Namespace = XMLL.namespace(“z”); // Create Namespace inorder to access the XML node <z:
var sNS:Namespace = XMLL.namespace(“s”); // Create Namespace inorder to access the XML node <s:
var FinalXML:XMLList=new XMLList(XMLL.rsNS::data.zNS::row) // access the node <rs:row>
Alert.show(FinalXML.@ows_LinkTitle)

Normal XML

<xml >
<data>
<row ID=’1′ c1=’Add ‘/>
<row ID=’2′ c1=’Drop ‘/>
<row ID=’3′ c1=’Modify Date ‘/>
<row ID=’4′ c1=’Complete ‘/>
<row ID=’5′ c1=’Modify Parameters ‘/>
</data>
</xml>

var XMLL:XMLList=new XMLList(e.target.data)
var FinalXML:XMLList=new XMLList(XMLL.data.row)
Alert.show(FinalXML.@ows_LinkTitle)

Find and replace in ActionScript

Here i have given two functions

  • Find – Checks whether the string holder contains the word and returns true or false
  • FindAndReplace – Checks for the string and replace with the new word provided.

var Str=“i love blogging”
var
out=Find(Str,“love”)
trace
(“result —>” +out)
var
out1=FindAndReplace(Str,“love”,“hate”)
trace
(“result1 —->”+out1)

function Find(SourceStr, FindString) :Boolean
{
var NArray=SourceStr.split(FindString)
if(NArray.length==2)
return true
else

return false
}

function FindAndReplace(SourceString, FindString,ReplaceString) :String
{
var SplitArray=SourceString.split(FindString)
var Result=SplitArray.join(ReplaceString)
return
Result;
}

Save File in Adobe Flex -AIR

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”onCreate(event)”>
<mx:Script>
<![CDATA[
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
private
function onCreate(e:Event):
void
{

var file:File = File.desktopDirectory; //Set the Directory location to Desktop
file = file.resolvePath(
“test.txt); //File Name
var
fileStream:FileStream = new
FileStream();   // Create new FileStream to Perform Read or Write
fileStream.open(file, FileMode.WRITE); // Open file in WRITE Mode
fileStream.writeUTFBytes(
“Hello World”); //Write Hello World into test.txt
fileStream.close(); //Close the File Stream
}

]]>
</mx:Script>
</mx:WindowedApplication>


This code creates a file named test.txt with content “Hello World” in desktop folder.

The desktopDirectory property provides a way to reference the desktop directory that works across platforms. If you set a File object to reference the desktop directory using the nativePath or url property, it will only work on the platform for which that path is valid Similarly documentDirectory /applicationStorageDirectory /applicationDirectory can be used to represent the folder.

var file:File = new File();
file = file.resolvePath(
“c:\\test.txt”);

This will work in windows platform and stores file in user mentioned location.

Accessing SharePoint List as XML

1. Returning all data for a SharePoint list,
including its XSD –http://[localhost]/_vti_bin/owssvr.dll?Cmd=Display&amp;List={ListGuid}&amp;Query=*&amp;XMLDATA=TRUE

2.Rreturning data of SharePoint list based on a specific view from the list –http://[localhost]/_vti_bin/owssvr.dll?Cmd=Display&amp;List={ListGuid}&amp;View={ViewGuid}&amp;XMLDATA=TRUE

3.Returning List definition – http://[localhost]/_vti_bin/owssvr.dll?Cmd=ExportList&amp;List={ListGuid}

4. Retrieving ONET.XML – http://[localhost]/_vti_bin/owssvr.dll?Cmd=GetProjSchema

5. Retrieving field types – http://[localhost]/vti_bin/owssvr.dll?Cmd=GetProjSchema&amp;SiteTemplate=fldtypes