Thursday, October 18, 2007

Flex 2 - Read local File

This is a simple Flex 2 project that reads data from a local file and displays it on a text area on the click of the Fetch Data button. There are different ways in which you can capture data from a file, i.e, in simple String variable, an XMLDocument etc.

There's a trick to mention the URL of the file when the Data.txt file is not in the same folder as the main .mxml file. For example, if your file is in a folder called News, then the URLRequest should contain the location of the file like this:

"./News/Data.txt". Ofcourse, the News folder should be in the same domain. If you need to access a file in another domain, you will have to make a cross-domain file. This I will be discussing later.


The project structure should be like this for the below code to work:



This example shows a simple Data.txt file being read.



THE OUTPUT:

SOURCE CODE
Listing of LocalFileRead.mxml

3 comments:

Anonymous said...

Good for people to know.

Unknown said...

Hai,

I have a question about read.txt file.. I use './folder/bla.txt', when i 'Run #site' (Flex builder) it works just fine..
But when i 'Export Release Build' (Flex Builder) it didn't work anymore..
It just freeze when its load the txt file.. Do you maybe why it happens? en do you maybe have the solutions ?

Thank you!

Anonymous said...

try this it may help you-- it may be with security issues from accessing local file system. u can do -use-network=false in compiler arguments and another option is to deal with flash player Global security settings for more information about this global security settings plz try this link

http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html