Aug
12

Adobe's new Integrated Runtime (AIR) will be released at some point this year (a beta is currently available here). The release of AIR will bring new possibilities in how applications communicate with client machines. Previous to the Adobe Integrated Runtime, Flash applications adhered to a security model that drastically handicapped what your applications could and couldn't do. For more information on the general Flash player security model see this page; for more info on Flash player security and Flex 2.01 see this page. With AIR, all of the following are now possible:

The ability to:

  • create files and directories
  • list the contents of directories
  • copy and move files and directories
  • read and write text and binary files
  • serialize and deserialize ActionScript objects and classes to the file system
  • get system information on files and directories

In this blog post I'll talk about what new classes are available in Flex 3 (Flex "Moxie" M2 SDK to be specific) that make all this possible. In my next post I'll show an example of how to use the file system API in a real-world application.

Continue Reading

Aaron West's Gravatar
About this post:

This entry was posted by Aaron West on August 12, 2007 at 4:07 PM. It was filed in the following categories: Adobe AIR, Flex. It has been viewed 54598 times and has 14 comments.

14 Responses to Working With the File System API in AIR

  1. freska

    Hi, nice post. Too bad, I can't try it out. I've installed adobe AIR 1.0, but my Eclipse doesn't seem to recognize File System API. When I tried typing "import flash.filesystem.File", it says "undefined property File". Do you have any idea how to fix this? I use Eclipse 3.3 and Flex Builder 3. Thanks.

  2. Hi freska, based on your comments I take it you are running the plugin version of Flex Builder 3. From what I can tell, the plugin is only supported in the Eclipse 3.2 environment. Here's what I found on Adobe's site that leads me to believe this:

    (text below taken from the downloads page for Flex Builder 3 Beta)

    Flex Builder 3 Beta - Eclipse Plugin
    This download provides the Flex Builder 3 beta as an Eclipse plugin that can be used on either Macintosh or Windows platforms. The prerelease version of the Flex 3 SDK is included with this prerelease Flex Builder 3 download. Eclipse version 3.2.1 or higher is required in order to install and use this version of Flex Builder 3 beta.

  3. freska

    Yes, I am running the plugin version of Flex Builder 3. In fact, I'm also running the standalone version (Flex IDE). I tried downgrading to Eclipse 3.2, but that error is still being displayed. I find this from Adobe Flex 2 language Reference, "The flash.filesystem package contains classes used in accessing the filesystem. This package is only available to content running in the Adobe Integrated Runtime." Did I miss something related to the software spec or so?

  4. freska

    Haha.. it's my mistake. I did that on a Flex project, not AIR. You can delete my 2nd comment. Now, my Eclipse recognizes the "import flash.filesystem.File", but I still got the same error message. Say, when I tried your example #5, Eclipse says "undefined property fs" on "fs.close()". It also happens to other variables (myFile, myString) and the event handler method (fileWrittenComplete). Did I miss something here? Thanks.

  5. Glad you figured out the issue between a Flex project versus an AIR project. As far as the code errors go, the error messages indicate the variables are not defined - but you already know that. Exactly why the compiler thinks they are not defined is anyone's guess at this point. If you post the code you are using I might be able to help further.

    Lastly, in looking at my final example above (asynchronously writing a file) there may be an issue with the fs.close(); method call. Since the file stream is opened asynchronously the close() method may be getting called out of turn. It would be best to move the fs.close() into the fileWrittenComplete function (assuming all we wanted to do was write to the file and nothing else). If we were going to perform several write actions on the file, the fs.close() method could be strategically placed somewhere else.

  6. freska

    Hi Aaron, sorry for taking so long. I had another task to complete.

    Well, I just put your code right away inside the <mx:Script> tag without adding anything. The errors, saying something about undefined property, show up when I save the file. Now, I try putting the code inside a function and have it called when a button is clicked. The errors are gone and a file is created successfully on my desktop.

  7. Jesse

    I am developing my first Flex application, and I don't know anything about AIR.

    You say that "A new package is included in Flex 3 called flash.filesystem," yet I am trying to compile a Flex 3.2 application that references this package per your synchronously example and I get compiler errors suggesting the types are not found:

    Error: Type was not found or was not a compile-time constant: File.
    var docsDirectory:File = File.documentsDirectory;

    Error: Type was not found or was not a compile-time constant: File.
    var myFile:File = File.desktopDirectory.resolve("MyFile.txt");

    Error: Type was not found or was not a compile-time constant: FileStream.
    var fs:FileStream = new FileStream();

    Error: Access of undefined property File.
    var docsDirectory:File = File.documentsDirectory;

    Error: Definition flash.filesystem could not be found.
    import flash.filesystem.*;

    Error: Access of undefined property File.
    var myFile:File = File.desktopDirectory.resolve("MyFile.txt");

    Error: Call to a possibly undefined method FileStream.
    var fs:FileStream = new FileStream();

    Error: Access of undefined property FileMode.
    fs.open(myFile, FileMode.WRITE);

    How do I include the flash.filesystem package (beyond adding the line "import flash.filesystem.*;")? Should I be trying to compile an AIR application? If so, how do I do that?

  8. @Jesse - Are you working with a Flex project or an AIR project? If you are using the standalone Flex Builder 3 or the Flex Builder 3 Eclipse plugin, you can either create a Flex project or an AIR project.

    Only AIR projects have the ability to import the flash.filesystem package. Reading from and writing to the local file system is a feature in desktop (AIR) Flex applications only due to security restrictions in browser-based applications.

    In Flex Builder 3 and Eclipse each project will have an icon that tells you what type of project it is. AIR projects will have an AIR icon and Flex projects will have a Flex icon.

  9. bersy

    Hi Aaron, flash.filesystem.File class has Icon property, could you please advice how to use this one to display file's icon?

  10. @bersy - From the Flex 4 SDK documentation I found the following:

    An Icon object is an array of BitmapData objects corresponding to the various icon states. On Linux, the Icon object contains no icons. The following code shows how to find the image in the icon array that has the greatest height, and it sets a Bitmap object to that image.

    import flash.filesystem.File;
    import flash.display.*;

    var directory:File = File.documentsDirectory;
    var bitmaps:Array = directory.icon.bitmaps;
    var bmpData:BitmapData = new BitmapData(1, 1);
    for (var i:uint = 0; i < bitmaps.length; i++) {
    if (bitmaps[i].height > bmpData.height) {
    bmpData = directory.icon.bitmaps[i];
    }
    }
    var iconBmp:Bitmap = new Bitmap(bmpData);

    You might add this Bitmap object as a child of a display object container, such as a Sprite object or a Flex UIComponent object.

  11. bersy

    Thanks for the quick response. I certainly saw that example, but when I added a Bitmap using the addChild (as described in example) an error was occured. It turned out that the need using rawChildren.addchild, then everything works.

  12. khushwant

    Hi,

    I have created an AIR application using flash cs5.In that application when user clicks on download button files get copied to user's desktop from application directory. Its working fine on windows but when i test it on mac it is doing all the other task but when user clicks on download button then it is not copying the files from application directory to user's desktop.

    Please reply as soon as possible.

  13. khushwant

    Everything i am doing is locally. and application directory means USB drive(pen drive) because i have AIR installation folder in USB drive along with all other files(which needs to get copied to user's desktop when user clicks on download button)

  14. Sonic

    So you mean "AIR" app is AIR app created by "Flash Builder", instead of AIR app created by "Flash CS6" ?
    No way to use "FileStream" class in cs6?