Creating a FileReference object using MXML in Flex

by Peter deHaan on August 25, 2008

in FileReference

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 comments… read them below or add one }

1 Yevgen August 25, 2008 at 11:41 am

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

Reply

2 peterd August 25, 2008 at 11:53 am

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

Reply

3 Yevgen August 27, 2008 at 6:06 am

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.

Reply

4 peterd August 27, 2008 at 7:28 am

Yevgen,

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

Peter

Reply

5 pou October 28, 2008 at 8:36 pm

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.

Reply

6 Mack October 10, 2009 at 2:07 pm

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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: