Quantcast
Channel: Flex Examples » ImageSnapshot
Viewing all articles
Browse latest Browse all 6

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

$
0
0

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.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?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/ -->
<s:Application name="FileReference_save_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        xmlns:net="flash.net.*"
        creationComplete="init();">
 
    <fx:Script>
        <![CDATA[
            private function init():void {
                textArea.text = describeType(FileReference).toXMLString();
            }
 
            private function btn_click(evt:MouseEvent):void {
                fileReference.save(textArea.text, "describeType.txt");
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <net:FileReference id="fileReference" />
    </fx:Declarations>
 
    <s:Panel id="panel"
            width="500" height="300"
            verticalCenter="0" horizontalCenter="0">
        <s:controlBarContent>
            <s:Button id="btn"
                    label="Save Text"
                    click="btn_click(event);" />
        </s:controlBarContent>
        <s:TextArea id="textArea"
                editable="true"
                width="100%"
                height="100%" />
    </s:Panel>
 
</s: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:

<?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/ -->
<s:Application name="FileReference_save_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        xmlns:net="flash.net.*"
        creationComplete="init();">
 
    <fx: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");
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <net:FileReference id="fileReference" />
    </fx:Declarations>
 
    <s:Panel id="panel"
            width="500" height="300"
            verticalCenter="0" horizontalCenter="0">
        <s:controlBarContent>
            <s:Button id="btn"
                    label="Save"
                    click="btn_click(event);" />
        </s:controlBarContent>
        <s:TextArea id="textArea"
                editable="true"
                width="100%"
                height="100%" />
    </s:Panel>
 
</s: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:

<?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/ -->
<s:Application name="FileReference_save_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        xmlns:net="flash.net.*"
        creationComplete="init();">
 
    <fx: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");
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <net:FileReference id="fileReference" />
    </fx:Declarations>
 
    <s:Panel id="panel"
            width="500" height="300"
            verticalCenter="0" horizontalCenter="0">
        <s:controlBarContent>
            <s:Button id="btn"
                    label="Save"
                    click="btn_click(event);" />
        </s:controlBarContent>
        <s:TextArea id="textArea"
                editable="true"
                width="100%" height="100%" />
    </s:Panel>
 
</s: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.

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

 

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles



Latest Images