Loading a Text File in Flash

Just goes to show you you can teach an old dog new tricks. (Yes, old. I turned 40 the other day.) Here’s something I never knew you could do: load a plain text file in to Flash.

There are a number of types of files you can load into Flash – swfs, jpgs, xml, stylesheets, and LoadVars, to name a bunch. I never knew of a way of simply loading a plain text file though. LoadVars is the closest, but after loading, it is broken down into name/value pairs. My solution in the past was to preface the text file with a variable name and pull the text out of the variable: “info=blah blah blah”, then grab myLoadVars.info.

But, as I was perusing the Flash help files a couple of weeks ago, I came across an example of loading straight text. Basically, you use the LoadVars object, and the onData event. The onData handler gets passed a string of the data loaded. To quote: “A string or undefined; the raw (unparsed) data from a LoadVars.load() or LoadVars.sendAndLoad() method call.”

This allows you to load text in any format – html, css, csv, xml, you name it, and then do your own parsing on it. Opens up a lot of possibilities.

I know, I know, it’s not a new, undocumented feature that I just discovered or anything. Just kind of cool, and it surprises me that after five years of living and breathing Flash, there are still relatively major (and useful) things like this that I have missed.

This entry was posted in Flash. Bookmark the permalink.

14 Responses to Loading a Text File in Flash

  1. Josh says:

    You can do the same thing with the onData event of the XML object. I’ve used it to load up CSV-formatted data and then parse it myself in the onData handler.

  2. darron says:

    Theres a good example of this in Flash Hacks. I forget what it was (been awhile since I did tech editing for it), but it was like exporting the 3d coordinates from Maya or something, and pulling them in and rendering them in Flash using onData.

    Useful little tip… 🙂

  3. Keith Peters says:

    The worst thing is that I was just telling someone a couple of weeks back that there was no way to do that. 🙂

  4. Joe Martinez says:

    Thanks for the blog article on it. I do the varname= in front too. I hated that you could only have one var per file w/o url encoding them.

    PS: Extending Flash was excellent. I made a todo panel right off that you can use to store todo items in the fla. I am going to make functionality to allow you to prioritize them, etc. and release it. I had no idea what jsfl was before the book and now I’m making panels!

    Cheers
    Joe Martinez

  5. Abdul Qabiz says:

    As Josh said, I also used XML object most of times to load any type of text content (xml, txt, csv, html, css etc), just by overriding onData callback.

    I also wrote a CSV Parser class:

    http://forum.indiammug.com/viewtopic.php?t=83&highlight=

    I hope we can also load binary data and parse in flash :)…

    BTW! Darron loading that 3d (text)file and rendering in flash was kick ass experiment, I remember it was done in time of Flash 5…real inspiring stuff..where all inovation gone now…I don’t see much stuff happening now…

    cheers,
    Abdul

  6. Abdul Qabiz says:

    Sorry for posting again, Keith, are you really 40 yrs old? I don’t believe, may be I never saw that’s why…

    Or you sound very young 🙂

    cheers,
    Abdul

  7. JA says:

    Flash has been around for longer than 5 years. 😉

  8. Ryan Sarver says:

    Be sure to get all of your questions for Keith in soon, at his age you never know how long he will be around. 😉

    Glad to see you finally got your text file learn on.

    rs

  9. Keith Peters says:

    Ryan, still stalking me, eh? Don’t forget that restraining order. Damn groupies! 😉

  10. ELECTRON says:

    load binary data in flash ….
    i crash head with it past weekend, interesting ..that

    jpg_lv = new LoadVars();
    jpg_lv.onData = function(src){
    trace(jpg_lv.getBytesLoaded());// “16833” (bytes loaded!!!)
    trace(src);// “” (empty string!!)
    }
    jpg_lv.load(“232.jpg”);

    shows that getBytesLoaded(), real file size, but where that data is loaded?????
    really i come to it because it is not possible to post file on server with and form.file. click() and form. submit(), then i try to do some extreme like that to get contents of jpg and finally post it to php from flash, it’s seems like flash loads that binary data, but not undersand it or forget at time because it is not looks like some string-like…
    would be extremely great to parse it or have ability to do something , like post with it (:

    off-topic: finally, most interesting thing: why they (microsoft-super-duper-programmers) include that method click() on input_type=file ???
    http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/input_file.asp
    , what brings up browse for file dialog, when *USER CAN SELECT* file, and after it file cannot be uploaded to server, because “security reasons.. (how found around net)”.. i agree that possible for security reasons value of filename for upload should not be set from anywhere else except user select, but what problem is to submit it after USER select file – it’s mysterious secret of great and paranoic ms programmers…

    but one reason – it can be used on hidden layer to select local file with what you want to load somewhere, like flash..

    f***, it’s morning.. should go sleep a lot..

  11. anon says:

    Electron,

    The data is in the browser cache. What you are doing is a nice way to preload images into the user cache.

    Because the text objects in Flash are limited in byte range for parsing, they can’t possibly extract info from your jpeg, unless your jpeg image was hacked in a hex editor and contains ascii range characters.

  12. Roger says:

    It’s not an undocument feature anymore, probably since the 7.2 update. See the “LoadVars class” page in AS Dictionnary 🙂
    Bye

  13. Keith Peters says:

    Which is why I said “it’s not a new, undocumented feature that I just discovered or anything.” 🙂

Comments are closed.