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″.
<?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:
<?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:
<?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.