25
Aug
08

Saving files locally using the FileReference class’s save() method in Flash Player 10

The following example shows how you can use the FileReference class’s new save() method in Flash Player 10 to save text, XML, and image files to a user’s hard drive.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/ -->
<Application name="FileReference_save_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library:adobe/flex/halo"
        xmlns:net="flash.net.*"
        layout="flex.layout.BasicLayout"
        creationComplete="init();">

    <Script>
        <![CDATA[
            private function init():void {
                textArea.text = describeType(FileReference).toXMLString();
            }

            private function btn_click(evt:MouseEvent):void {
                fileReference.save(textArea.text, "describeType.txt");
            }
        ]]>
    </Script>

    <Declarations>
        <net:FileReference id="fileReference" />
    </Declarations>

    <mx:Panel id="panel"
            width="500"
            height="300"
            verticalCenter="0"
            horizontalCenter="0">
        <mx:TextArea id="textArea"
                editable="true"
                width="100%"
                height="100%" />
        <mx:ControlBar horizontalAlign="right">
            <Button id="btn"
                    label="Save Text"
                    click="btn_click(event);" />
        </mx:ControlBar>
    </mx:Panel>

</Application>

View source is enabled in the following example.

You can also pass the XML object directly to the FileReference class’s save() method, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/ -->
<Application name="FileReference_save_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library:adobe/flex/halo"
        xmlns:net="flash.net.*"
        layout="flex.layout.BasicLayout"
        creationComplete="init();">

    <Script>
        <![CDATA[
            private const xmlObj:XML = describeType(FileReference);

            private function init():void {
                textArea.text = xmlObj.toXMLString();
            }

            private function btn_click(evt:MouseEvent):void {
                fileReference.save(xmlObj, "describeType.xml");
            }
        ]]>
    </Script>

    <Declarations>
        <net:FileReference id="fileReference" />
    </Declarations>

    <mx:Panel id="panel"
            width="500"
            height="300"
            verticalCenter="0"
            horizontalCenter="0">
        <mx:TextArea id="textArea"
                editable="true"
                width="100%"
                height="100%" />
        <mx:ControlBar horizontalAlign="right">
            <Button id="btn"
                    label="Save"
                    click="btn_click(event);" />
        </mx:ControlBar>
    </mx:Panel>

</Application>

Or, you can use the ImageSnapshot class to take a screenshot of an item on the display list and save it to the user’s hard drive by passing a ByteArray object to the FileReference class’s save() method, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/ -->
<Application name="FileReference_save_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library:adobe/flex/halo"
        xmlns:net="flash.net.*"
        layout="flex.layout.BasicLayout"
        creationComplete="init();">

    <Script>
        <![CDATA[
            import mx.graphics.ImageSnapshot;
            import mx.graphics.codec.*;

            private const jpegEnc:JPEGEncoder = new JPEGEncoder();
            private const xmlObj:XML = describeType(FileReference);

            private function init():void {
                textArea.text = xmlObj.toXMLString();
            }

            private function btn_click(evt:MouseEvent):void {
                var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(panel, 0, jpegEnc);
                fileReference.save(imageSnap.data, "describeType.jpg");
            }
        ]]>
    </Script>

    <Declarations>
        <net:FileReference id="fileReference" />
    </Declarations>

    <mx:Panel id="panel"
            width="500"
            height="300"
            verticalCenter="0"
            horizontalCenter="0">
        <mx:TextArea id="textArea"
                editable="true"
                width="100%"
                height="100%" />
        <mx:ControlBar horizontalAlign="right">
            <Button id="btn"
                    label="Save"
                    click="btn_click(event);" />
        </mx:ControlBar>
    </mx:Panel>

</Application>

View source is enabled in the following example.

For more information on the new FileReference capabilities in Flash Player 10, see the Flex Gumbo documentation at http://livedocs.adobe.com/flex/gumbo/langref/flash/net/FileReference.html.


2 Responses to “Saving files locally using the FileReference class's save() method in Flash Player 10”


  1. 1 alex Sep 24th, 2008 at 10:53 am

    perfect THX this help me sooooooo much… this is the first working example I’ve found… this whole site is awesome.. thx for your work

  2. 2 peterd Sep 24th, 2008 at 1:07 pm

    Disclaimer, this entry was written with a beta version of Flash Player 10 and Flex Gumbo. This code could change/break at any point in the future.

    For example, I believe that this:

    <Application xmlns="http://ns.adobe.com/mxml/2009"
            layout="flex.layout.BasicLayout">
    ...
    </Application>
    

    May now need to be changed to this:

    <Application xmlns="http://ns.adobe.com/mxml/2009">
        <layout><BasicLayout /></layout>
    ...
    </Application>
    

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".