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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

6 Responses to Creating a FileReference object using MXML in Flex

  1. Yevgen says:

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

  2. peterd says:

    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. Yevgen says:

    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. peterd says:

    Yevgen,

    Sorry, it is the default Flash Player detection code from Flex Builder 3.0.1.

    Peter

  5. pou says:

    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.

  6. Mack says:

    What about an upload? Were you able to figure out how to do that?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree