Saturday, 10 October 2009

Thinking in Flex, local files

Ok, this is not a post related to Bruce Eckel’s hihgly reputed books like C++ or Java, just the title is incluenced from them.

Working with Flex can be a little trickier for regular web developers, specially for the ones who never ever worked with Java Applets. So what flex is and what is flex not, first flex is not a traditional web application and its definitly not a desktop application. Actually flex is a more skilled web application which is pretending to be a desktop application. A widely asked question to me is local filesystem access in Flex, which is something you can't do and also you should not in normal conditions. Since you are developing a web project why would you need to write local files and if you really need this why are you developing a web project instead of a desktop one? I am sure anyone can give pages of reasons but still try to think in Flex, how will you be sure about the security of the file, if its modified externally or what if you rely on that and it just disappears..

However, Flex still offers writing limited local files which are called SharedObjects. First of all SharedObjects are simple, and they should be used for simple tasks in a simple way. Writing to a SharedObject is not like a regular file I/O operation for the developers perspective. You just ask one to be created for you;

var myPrefs:SharedObject = SharedObject.getLocal("MuratsPrefs");

and use it in a simple way by accessing its data object to read or store your values;"Murat";"3.10.2009";"chrome";

simply commit your changes;


or clear whatever it has;


Actually the file is much or less like a cookie and also referred as a Flash Cookie on TechNote of Adobe. This file is limited to 100kb by default and would ask for permission of the file is exceeds. However this is a bit tricky since the user can modify the size, even to 0kb and can also disable the permission popup which would result blocking the whole access.

I couldn't manage to locate the file on Mac. I tried to load the written file from another application which is built as another swf, and could not get any data. Next i tried to load the same file from the same flex application running in a different browser which also failed.

SharedObjects seems a nice and easy to use tool but they can not guarantee the access, if you need the change your application to a new swf the file would be lost, and suprisingly worst if the client decides to change the browser, still your file would not be reachable. So use them at your own risk.