Creating a FileReference object using MXML in Flex

The following example shows how you can define a FileReference object in MXML by defining a custom namespace for the flash.net package.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/ -->
<mx:Application name="FileReference_download_test_2"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        xmlns:net="flash.net.*"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private const DOWNLOAD_URL:String = "http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test_2/bin/srcview/FileReference_download_test_2.zip";

            private function btn_click(evt:MouseEvent):void {
                var urlReq:URLRequest = new URLRequest(DOWNLOAD_URL);
                fileReference.download(urlReq);
            }

            private function onEvent(evt:Event):void {
                Alert.show(evt.toString(), evt.type);
            }
        ]]>
    </mx:Script>

    <net:FileReference id="fileReference"
            complete="onEvent(event);"
            cancel="onEvent(event);"
            httpStatus="onEvent(event);"
            ioError="onEvent(event);" />

    <mx:Button id="btn"
            label="Download source"
            click="btn_click(event);" />

</mx:Application>

View source is enabled in the following example.

6 thoughts on “Creating a FileReference object using MXML in Flex

  1. Could you fix Flash plugin validator?
    I tried IE7 and FF3 with player 9.0r115 and got request to instal Flash Player 9

  2. Yevgen,

    Yeah, sorry. I believe that Flex Builder 3.0.1 update targets Flash Player 9,0,124,0 by default. So you may need to update your Flash Player to 9,0,124,0, or I may have to set the target Flash Player to 9,0,115,0 and republish each of the samples from the last week or so.

    In the future I’ll try and target a lower version of the Flash Player.

    Peter

  3. Thanks. I updated my player. But you didn’t count other problem.
    Your visitors in the most cases are developers. Your “download player” link pointed to regular player not debug version.

  4. Sorry, I have a question, it about …
    When user click download button, application doesn’t open dialog box to user select save path, then the applicaton direct download file and save the specific path.

    above is my case. I don’t know how to solve the question..

    can you give some idea to me, thank you.

Comments are closed.