Blog



Its quite hard to believe that such a simple thing – download a file from server in flex is such a pain!

The code was very simple, from the manual and example:

var fileRef:FileReference = new FileReference();
fileRef.download(new URLRequest("myFile.txt"), "myFile.txt");

But in real world, there are things that they didn’t tell unless you hit it!
From flash player 10, all uploads/downloads in fileRef won’t allowed seamlessly. It require user to click a button or hotkey to invoke upload/download action. Otherwise it will give you an error #2167. This isn’t difficult to resolve if it’s not too much of a pain for your user to click a button one more time.

Alert.show("Click OK to download.",null,4,null,startDownload);
 
function startDownload():void{
		var fileRef:FileReference;
		fileRef = new FileReference();
		const FILE_URL:String = "myTextFile.txt";
		urlReq = new URLRequest(FILE_URL);
		urlReq.method = URLRequestMethod.GET;
		try
			{
				fileRef.download(urlReq);
			}
			catch (error:Error)
			{
				Alert.show(error.message);
			}
	}
}

I thought this could work, not until I found that my previous post… you need to define the fileReference outside of the function… otherwise it won’t!
Finally… this would work:

var fileRef:FileReference;
fileRef = new FileReference();
Alert.show("Click OK to download.",null,4,null,startDownload);
function startDownload():void{
 
		const FILE_URL:String = "myTextFile.txt";
		urlReq = new URLRequest(FILE_URL);
		urlReq.method = URLRequestMethod.GET;
		try
			{
				fileRef.download(urlReq);
			}
			catch (error:Error)
			{
				Alert.show(error.message);
			}
 
}

Problem in downloading file

5.04.2010

POSTED IN Programming TAGS :

Click a button and download a file in Flex is very simple but Adobe’s official example has a problem which made me spent like 20 mins to find a work around.
This won’t work:

var request:URLRequest = new URLRequest("index.xml");
var fileRef:FileReference = new FileReference();
fileRef.download(request);

But this will work:

var fileRef:FileReference = new FileReference();
function downloadFile():void {   
fileRef.download(new URLRequest("index.xml"));
}
downloadFile();

Pass by Value

5.04.2010

POSTED IN Programming TAGS :

Actionscript 3 will always pass by reference unless the assignation is a primitive type (such as integer or string). I had an array and want to pass it by value (means it will copy the whole array’s value in it instead of referencing). It is because referencing will affect the referenced array when the new array is being changed in value.I came across with a script to deal with it. it may not be perfect, but have a look:

public function clone(source:Object):*{
  var myBA:ByteArray = new ByteArray();
  myBA.writeObject(source);
  myBA.position = 0;
  return(myBA.readObject());
}

Be aware that your objects in your array (or object)’s type may changed to Object/Array instead of it’s previous type.

UPDATE: There is also a method in Flex ObjectUtil class called “copy” ObjectUtil.copy(); which copies the specified Object and returns a reference to the copy.

Hopefully this is helpful to someone.

I had some troubles to re-load my itemrenderer. When the dataprovider of your collection (Tilebase or whatever) chage, the itemrenderer of exists items does not re-init, if your code is in creationComplete event. Finally I found a solution:in your itemrenderer file, on the creationComplete handler, add:

this.addEventListener(FlexEvent.DATA_CHANGE, dataChanged);dataChanged();

Then, in the dataChange function, you can do whatever you want for your itemrenderer when your dataprovider change.Hope this will help someone.

UPDATED – About me

5.04.2010

POSTED IN Blog, Site News

Finally a more proper “About me” section is updated. I really like the effects put on together. check it out here.

hope you guys like this more :)

I’ve help the company design from scratch. Their website, name cards, corporate identify and advertising material. I like the clean and flash animated website. Really happy to work with nice people!

Stem Cell International

New site launched!

13.01.2010

POSTED IN Blog, Site News

Thank you for your interest reading this website. It’s a very clean and minimalist design. I hope you will like it. I’d like to use this to feature my previous and future work.

Currently available for freelance

Let me know if I could be in any help. Feel free to drop me an email: alan(at)hkalan.com or contact me