<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex Examples &#187; FileReference</title>
	<atom:link href="http://blog.flexexamples.com/category/filereference/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Previewing an image before uploading it using the FileReference class in Flash Player 10</title>
		<link>http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/</link>
		<comments>http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 04:38:04 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[beta]]></category>
		<category><![CDATA[FileFilter]]></category>
		<category><![CDATA[FileReference]]></category>
		<category><![CDATA[browse()]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[Gumbo]]></category>
		<category><![CDATA[load()]]></category>
		<category><![CDATA[needsSWF]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/</guid>
		<description><![CDATA[<p>The following example shows how you can browse for an image file from your local file system and preview the image before uploading it to a remote webserver by using the FileReference class&#8217;s new load() method in Flash Player 10. Once the user has browsed and selected an image from their local machine, you can [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can browse for an image file from your local file system and preview the image before uploading it to a remote webserver by using the FileReference class&#8217;s new <code>load()</code> method in Flash Player 10. Once the user has browsed and selected an image from their local machine, you can call the <code>load()</code> method which dispatches a complete event when the image has successfully loaded, at which point you can display the image using a Flex Image control and setting the Image instance&#8217;s <code>source</code> property to the FileReference class&#8217;s <code>data</code> property (which is a ByteArray).</p>
<p>Full code after the jump.</p>
<p><span id="more-766"></span></p>
<p class="alert">The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see <a href="http://bit.ly/crThlI">http://www.adobe.com/products/flex/</a>. To download the latest nightly build of the Flex 4 SDK, see <a href="http://bit.ly/Flex4SDK">http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4</a>.<br/><strong>For more information on getting started with Flex 4 and Flash Builder 4, see the official <a href="http://bit.ly/dCkecm">Adobe Flex Team blog</a>.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> name=<span style="color: #ff0000;">&quot;FileReference_load_test&quot;</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span></span>
<span style="color: #000000;">        xmlns:net=<span style="color: #ff0000;">&quot;flash.net.*&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            import mx.controls.Alert;</span>
<span style="color: #000000;">            import mx.utils.ObjectUtil;</span>
&nbsp;
<span style="color: #000000;">            private function btn_click<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                var arr:Array = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;</span>
<span style="color: #000000;">                arr.push<span style="color: #66cc66;">&#40;</span>new FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Images&quot;</span>, <span style="color: #ff0000;">&quot;.gif;*.jpeg;*.jpg;*.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">                fileReference.browse<span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">            private function fileReference_select<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                fileReference.load<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">            private function fileReference_complete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                img.source = fileReference.data;</span>
<span style="color: #000000;">                Alert.show<span style="color: #66cc66;">&#40;</span>ObjectUtil.toString<span style="color: #66cc66;">&#40;</span>fileReference<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;net:FileReference</span> id=<span style="color: #ff0000;">&quot;fileReference&quot;</span></span>
<span style="color: #000000;">                select=<span style="color: #ff0000;">&quot;fileReference_select(event);&quot;</span></span>
<span style="color: #000000;">                complete=<span style="color: #ff0000;">&quot;fileReference_complete(event);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Panel</span> id=<span style="color: #ff0000;">&quot;panel&quot;</span></span>
<span style="color: #000000;">            layout=<span style="color: #ff0000;">&quot;absolute&quot;</span></span>
<span style="color: #000000;">            horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #000000;">            verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #000000;">            width=<span style="color: #ff0000;">&quot;500&quot;</span></span>
<span style="color: #000000;">            height=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Image</span> id=<span style="color: #ff0000;">&quot;img&quot;</span></span>
<span style="color: #000000;">                verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #000000;">                horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #000000;">                maxWidth=<span style="color: #ff0000;">&quot;200&quot;</span></span>
<span style="color: #000000;">                maxHeight=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:ControlBar</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> id=<span style="color: #ff0000;">&quot;btn&quot;</span></span>
<span style="color: #000000;">                    label=<span style="color: #ff0000;">&quot;Browse and preview...&quot;</span></span>
<span style="color: #000000;">                    click=<span style="color: #ff0000;">&quot;btn_click(event);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> label=<span style="color: #ff0000;">&quot;Upload...&quot;</span></span>
<span style="color: #000000;">                    enabled=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:ControlBar</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Panel</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_load_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/FileReference_load_test/bin/main.html" width="100%" height="350"></iframe></p>
<p>For more information on the new FileReference capabilities in Flash Player 10, see the Flex Gumbo documentation at <a href="http://livedocs.adobe.com/flex/gumbo/langref/flash/net/FileReference.html">http://livedocs.adobe.com/flex/gumbo/langref/flash/net/FileReference.html</a>.</p>
<p class="alert">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.</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Previewing an image before uploading it using the FileReference class in Flash Player 10 on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/',contentID: 'post-766',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'browse(),data,Gumbo,load(),needsSWF',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/feed/</wfw:commentRss>
		<slash:comments>64</slash:comments>
		</item>
		<item>
		<title>Saving files locally using the FileReference class&#8217;s save() method in Flash Player 10</title>
		<link>http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/</link>
		<comments>http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 04:18:14 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[FileReference]]></category>
		<category><![CDATA[Flex4]]></category>
		<category><![CDATA[ImageSnapshot]]></category>
		<category><![CDATA[captureImage()]]></category>
		<category><![CDATA[Gumbo]]></category>
		<category><![CDATA[JPEGEncoder]]></category>
		<category><![CDATA[save()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/</guid>
		<description><![CDATA[<p>The following example shows how you can use the FileReference class&#8217;s new save() method in Flash Player 10 to save text, XML, and image files to a user&#8217;s hard drive.</p> <p></p> &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;!-- http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/ --&#62; &#60;s:Application name=&#34;FileReference_save_test&#34; xmlns:fx=&#34;http://ns.adobe.com/mxml/2009&#34; xmlns:s=&#34;library://ns.adobe.com/flex/spark&#34; xmlns:mx=&#34;library://ns.adobe.com/flex/mx&#34; xmlns:net=&#34;flash.net.*&#34; creationComplete=&#34;init();&#34;&#62; &#160; &#60;fx:Script&#62; &#60;!&#91;CDATA&#91; private function init&#40;&#41;:void &#123; textArea.text = describeType&#40;FileReference&#41;.toXMLString&#40;&#41;; &#125; [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can use the FileReference class&#8217;s new <code>save()</code> method in Flash Player 10 to save text, XML, and image files to a user&#8217;s hard drive.</p>
<p><span id="more-767"></span></p>
<p class="alert">The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see <a href="http://bit.ly/crThlI">http://www.adobe.com/products/flex/</a>. To download the latest nightly build of the Flex 4 SDK, see <a href="http://bit.ly/Flex4SDK">http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4</a>.<br/><strong>For more information on getting started with Flex 4 and Flash Builder 4, see the official <a href="http://bit.ly/dCkecm">Adobe Flex Team blog</a>.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> name=<span style="color: #ff0000;">&quot;FileReference_save_test&quot;</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span> </span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span></span>
<span style="color: #000000;">        xmlns:net=<span style="color: #ff0000;">&quot;flash.net.*&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;init();&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            private function init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                textArea.text = describeType<span style="color: #66cc66;">&#40;</span>FileReference<span style="color: #66cc66;">&#41;</span>.toXMLString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">            private function btn_click<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                fileReference.save<span style="color: #66cc66;">&#40;</span>textArea.text, <span style="color: #ff0000;">&quot;describeType.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;net:FileReference</span> id=<span style="color: #ff0000;">&quot;fileReference&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> id=<span style="color: #ff0000;">&quot;panel&quot;</span></span>
<span style="color: #000000;">            width=<span style="color: #ff0000;">&quot;500&quot;</span> height=<span style="color: #ff0000;">&quot;300&quot;</span></span>
<span style="color: #000000;">            verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:controlBarContent</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;btn&quot;</span></span>
<span style="color: #000000;">                    label=<span style="color: #ff0000;">&quot;Save Text&quot;</span></span>
<span style="color: #000000;">                    click=<span style="color: #ff0000;">&quot;btn_click(event);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:controlBarContent</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextArea</span> id=<span style="color: #ff0000;">&quot;textArea&quot;</span></span>
<span style="color: #000000;">                editable=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">                width=<span style="color: #ff0000;">&quot;100%&quot;</span></span>
<span style="color: #000000;">                height=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="information"><a href="http://dl.dropbox.com/u/4509565/FlexExamples_com/FileReference_save_test/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://dl.dropbox.com/u/4509565/FlexExamples_com/FileReference_save_test/main.html" width="100%" height="400"></iframe></p>
<p>You can also pass the XML object directly to the FileReference class&#8217;s <code>save()</code> method, as seen in the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> name=<span style="color: #ff0000;">&quot;FileReference_save_test&quot;</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span> </span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span></span>
<span style="color: #000000;">        xmlns:net=<span style="color: #ff0000;">&quot;flash.net.*&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;init();&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            private const xmlObj:XML = describeType<span style="color: #66cc66;">&#40;</span>FileReference<span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #000000;">            private function init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                textArea.text = xmlObj.toXMLString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">            private function btn_click<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                fileReference.save<span style="color: #66cc66;">&#40;</span>xmlObj, <span style="color: #ff0000;">&quot;describeType.xml&quot;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;net:FileReference</span> id=<span style="color: #ff0000;">&quot;fileReference&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> id=<span style="color: #ff0000;">&quot;panel&quot;</span></span>
<span style="color: #000000;">            width=<span style="color: #ff0000;">&quot;500&quot;</span> height=<span style="color: #ff0000;">&quot;300&quot;</span></span>
<span style="color: #000000;">            verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:controlBarContent</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;btn&quot;</span></span>
<span style="color: #000000;">                    label=<span style="color: #ff0000;">&quot;Save&quot;</span></span>
<span style="color: #000000;">                    click=<span style="color: #ff0000;">&quot;btn_click(event);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:controlBarContent</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextArea</span> id=<span style="color: #ff0000;">&quot;textArea&quot;</span></span>
<span style="color: #000000;">                editable=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">                width=<span style="color: #ff0000;">&quot;100%&quot;</span></span>
<span style="color: #000000;">                height=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Or, you can use the ImageSnapshot class to take a screenshot of an item on the display list and save it to the user&#8217;s hard drive by passing a ByteArray object to the FileReference class&#8217;s <code>save()</code> method, as seen in the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> name=<span style="color: #ff0000;">&quot;FileReference_save_test&quot;</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span> </span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span></span>
<span style="color: #000000;">        xmlns:net=<span style="color: #ff0000;">&quot;flash.net.*&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;init();&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            import mx.graphics.ImageSnapshot;</span>
<span style="color: #000000;">            import mx.graphics.codec.*;</span>
&nbsp;
<span style="color: #000000;">            private const jpegEnc:JPEGEncoder = new JPEGEncoder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            private const xmlObj:XML = describeType<span style="color: #66cc66;">&#40;</span>FileReference<span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #000000;">            private function init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                textArea.text = xmlObj.toXMLString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">            private function btn_click<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                var imageSnap:ImageSnapshot = ImageSnapshot.captureImage<span style="color: #66cc66;">&#40;</span>panel, <span style="color: #cc66cc;">0</span>, jpegEnc<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">                fileReference.save<span style="color: #66cc66;">&#40;</span>imageSnap.data, <span style="color: #ff0000;">&quot;describeType.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;net:FileReference</span> id=<span style="color: #ff0000;">&quot;fileReference&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> id=<span style="color: #ff0000;">&quot;panel&quot;</span></span>
<span style="color: #000000;">            width=<span style="color: #ff0000;">&quot;500&quot;</span> height=<span style="color: #ff0000;">&quot;300&quot;</span></span>
<span style="color: #000000;">            verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:controlBarContent</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;btn&quot;</span></span>
<span style="color: #000000;">                    label=<span style="color: #ff0000;">&quot;Save&quot;</span></span>
<span style="color: #000000;">                    click=<span style="color: #ff0000;">&quot;btn_click(event);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:controlBarContent</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextArea</span> id=<span style="color: #ff0000;">&quot;textArea&quot;</span></span>
<span style="color: #000000;">                editable=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">                width=<span style="color: #ff0000;">&quot;100%&quot;</span> height=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="information"><a href="http://dl.dropbox.com/u/4509565/FlexExamples_com/FileReference_save_test/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://dl.dropbox.com/u/4509565/FlexExamples_com/FileReference_save_test/main3.html" width="100%" height="400"></iframe></p>
<p>For more information on the new FileReference capabilities in Flash Player 10, see the Flex Gumbo documentation at <a href="http://livedocs.adobe.com/flex/gumbo/langref/flash/net/FileReference.html">http://livedocs.adobe.com/flex/gumbo/langref/flash/net/FileReference.html</a>.</p>
<p class="alert">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.</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Saving files locally using the FileReference class\&#039;s save() method in Flash Player 10 on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/',contentID: 'post-767',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'captureImage(),Gumbo,JPEGEncoder,save()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Creating a FileReference object using MXML in Flex</title>
		<link>http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/</link>
		<comments>http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 14:16:03 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[FileReference]]></category>
		<category><![CDATA[download]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/</guid>
		<description><![CDATA[<p>The following example shows how you can define a FileReference object in MXML by defining a custom namespace for the flash.net package.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test_2/bin/srcview/source/main.mxml.html">View MXML</a></p> &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;!-- http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/ --&#62; &#60;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"&#62; &#60;mx:Script&#62; &#60;![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 [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can define a FileReference object in MXML by defining a custom namespace for the flash.net package.</p>
<p>Full code after the jump.</p>
<p><span id="more-765"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test_2/bin/srcview/source/main.mxml.html">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/ --&gt;
&lt;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"&gt;

    &lt;mx:Script&gt;
        &lt;![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);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;net:FileReference id="fileReference"
            complete="onEvent(event);"
            cancel="onEvent(event);"
            httpStatus="onEvent(event);"
            ioError="onEvent(event);" /&gt;

    &lt;mx:Button id="btn"
            label="Download source"
            click="btn_click(event);" /&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test_2/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test_2/bin/main.html" width="100%" height="300"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Creating a FileReference object using MXML in Flex on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/',contentID: 'post-765',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'download',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using for the FileReference class&#8217;s uploadCompleteData event to capture data from a server-side script</title>
		<link>http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/</link>
		<comments>http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 05:57:30 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[FileReference]]></category>
		<category><![CDATA[upload()]]></category>
		<category><![CDATA[uploadCompleteData]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/</guid>
		<description><![CDATA[<p>The previous example, <a href="http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/">&#8220;Using the URLVariables and FileReference classes to pass data from Flex to a server-side script&#8221;</a>, showed how you could use the upload() method in the FileReference class along with the URLRequest and URLVariables classes to send data from your Flex application to a server-side script. Well, what happens if you want [...]]]></description>
			<content:encoded><![CDATA[<p>The previous example, <a href="http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/">&#8220;Using the URLVariables and FileReference classes to pass data from Flex to a server-side script&#8221;</a>, showed how you could use the <code>upload()</code> method in the FileReference class along with the URLRequest and URLVariables classes to send data from your Flex application to a server-side script. Well, what happens if you want to send data from your server-side script back to your Flex application? Say hello to the <code>uploadCompleteData</code> event! This event gets dispatched after data is received from the server after a successful upload.</p>
<p class="note">The <code>uploadCompleteData</code> event is not dispatched if data is not returned from the server.</p>
<p>The following example shows how you can set up an event listener for the <code>uploadCompleteData</code> event to display information about the file upload.</p>
<p>Full code after the jump.</p>
<p><span id="more-260"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_uploadCompleteData_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!-- http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/ --&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
        layout=&quot;vertical&quot;
        verticalAlign=&quot;middle&quot;
        backgroundColor=&quot;white&quot;
        creationComplete=&quot;init();&quot;&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import flash.net.FileReference;
            import mx.controls.Alert;
            import mx.utils.StringUtil;

            private var fileRef:FileReference;
            private var urlReq:URLRequest;

            private function init():void {
                fileRef = new FileReference();
                fileRef.addEventListener(Event.SELECT, fileRef_select);
                fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
                fileRef.addEventListener(IOErrorEvent.IO_ERROR, fileRef_ioError);
                fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileRef_uploadCompleteData);

                urlReq = new URLRequest();
                urlReq.url = <span style="color:red;">&quot;http://localhost:8300/fileref/uploader.cfm&quot;</span>;
            }

            private function fileRef_uploadCompleteData(evt:DataEvent):void {
                var strData:String = StringUtil.trim(evt.data);
                var vars:URLVariables = new URLVariables(strData);
                Alert.show(vars.fileName, &quot;fileName&quot;);
            }

            private function start():void {
                fileRef.browse();
            }

            private function fileRef_select(evt:Event):void {
                fileRef.upload(urlReq);
            }

            private function fileRef_complete(evt:Event):void {
                Alert.show(evt.toString(), evt.type);
            }

            private function fileRef_ioError(evt:IOErrorEvent):void {
                Alert.show(evt.text, evt.type);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Button label=&quot;upload&quot; click=&quot;start();&quot; /&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_uploadCompleteData_test/bin/srcview/index.html">View source</a></p>
<p>The previous example just uploads to http://localhost/ (in my case my local JRun server). Since that is pretty much useless 99.8% of the time, you would need to change that URL to your ColdFusion/PHP/ASP/Java upload script on your own server.</p>
<p>If you&#8217;re a ColdFusion&#8217;er, you can see my upload script in the previous entry, <a href="http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/">&#8220;Using the URLVariables and FileReference classes to pass data from Flex to a server-side script&#8221;</a>. Note that the script has the following lines, which return some name/value pairs to our Flex application. Flex will receive the data as a string, but you can easily convert the string into a URLVariables object by passing the string to the URLVariables constructor, or by using the URLVariables class&#8217;s <code>decode()</code> method. Once converted, Flex will have access to two variables from the ColdFusion script, <code>fileName</code> and <code>fileSize</code>. The <code>fileName</code> variable contains the name of the uploaded file from the server. Since the ColdFusion &lt;cffile /&gt; tag used a <code>nameConflict</code> attribute of &#8220;MAKEUNIQUE&#8221;, the server will generate a unique file name if a file by that name already exists in the specified directory. The <code>fileSize</code> variable contains the sie of the upload file from the server. Also, note that unlike ActionScript, ColdFusion is <em>not</em> case sensitive.</p>
<pre class="code">
&lt;cfoutput&gt;fileName=#CFFILE.serverFile#&amp;fileSize=#CFFILE.fileSize#&lt;/cfoutput&gt;
</pre>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Using for the FileReference class\&#039;s uploadCompleteData event to capture data from a server-side script on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/',contentID: 'post-260',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'upload(),uploadCompleteData',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Using the URLVariables and FileReference classes to pass data from Flex to a server-side script</title>
		<link>http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/</link>
		<comments>http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 02:00:11 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[FileReference]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[URLVariables]]></category>
		<category><![CDATA[getTimer()]]></category>
		<category><![CDATA[upload()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/</guid>
		<description><![CDATA[<p>We&#8217;ve already seen examples of using Flex to upload and download files (see <a href="http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/">&#8220;Uploading files in Flex using the FileReference class&#8221;</a> and <a href="http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/">&#8220;Downloading files in Flex using the FileReference class&#8221;</a>), but I&#8217;ve seen a lot of bugs/questions centering around the FileReference class lately so thought I&#8217;d try and do another example or two.</p> [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve already seen examples of using Flex to upload and download files (see <a href="http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/">&#8220;Uploading files in Flex using the FileReference class&#8221;</a> and <a href="http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/">&#8220;Downloading files in Flex using the FileReference class&#8221;</a>), but I&#8217;ve seen a lot of bugs/questions centering around the FileReference class lately so thought I&#8217;d try and do another example or two.</p>
<p>The following example shows how you can use a combination of the URLVariables, URLRequest, and FileReference classes to pass GET or POST variables to a server-side script while doing a file upload. In this example, we just happen to pass a simple text userID and the user&#8217;s Flash Player version, but it should be easy enough to modify the script slightly to pass more unique information to the server (such as a unique user token stored in a SharedObject or a browser cookie). I also added a crude little timer on the example to make it a bit classier.</p>
<p>Full code after the jump.</p>
<p><span id="more-259"></span></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        layout=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #000000;">        verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">        backgroundColor=<span style="color: #ff0000;">&quot;white&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;init();&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">        &lt;![CDATA[</span>
<span style="color: #339933;">            import flash.net.FileReference;</span>
<span style="color: #339933;">            import flash.net.URLRequestMethod;</span>
<span style="color: #339933;">            import mx.controls.Alert;</span>
<span style="color: #339933;">            import mx.utils.StringUtil;</span>
&nbsp;
<span style="color: #339933;">            private var fileRef:FileReference;</span>
<span style="color: #339933;">            private var urlVars:URLVariables;</span>
<span style="color: #339933;">            private var urlReq:URLRequest;</span>
<span style="color: #339933;">            private var startTimer:Number;</span>
<span style="color: #339933;">            private var timer:Timer;</span>
&nbsp;
<span style="color: #339933;">            private function init():void {</span>
<span style="color: #339933;">                fileRef = new FileReference();</span>
<span style="color: #339933;">                fileRef.addEventListener(Event.SELECT, fileRef_select);</span>
<span style="color: #339933;">                fileRef.addEventListener(Event.COMPLETE, fileRef_complete);</span>
<span style="color: #339933;">                fileRef.addEventListener(IOErrorEvent.IO_ERROR, fileRef_ioError);</span>
&nbsp;
<span style="color: #339933;">                urlVars = new URLVariables();</span>
<span style="color: #339933;">                urlVars.userID = 94103;</span>
<span style="color: #339933;">                urlVars.fpVersion = flash.system.Capabilities.version;</span>
&nbsp;
<span style="color: #339933;">                urlReq = new URLRequest();</span>
<span style="color: #339933;">                urlReq.method = URLRequestMethod.POST;</span>
<span style="color: #339933;">                urlReq.data = urlVars;</span>
<span style="color: #339933;">                urlReq.url = &quot;http://localhost:8300/fileref/uploader.cfm&quot;;</span>
&nbsp;
<span style="color: #339933;">                timer = new Timer(100);</span>
<span style="color: #339933;">                timer.addEventListener(TimerEvent.TIMER, onTimer);</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function onTimer(evt:TimerEvent):void {</span>
<span style="color: #339933;">                lbl.text = String(getTimer() - startTimer) + &quot; ms&quot;;</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function start():void {</span>
<span style="color: #339933;">                fileRef.browse();</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function fileRef_select(evt:Event):void {</span>
<span style="color: #339933;">                fileRef.upload(urlReq);</span>
<span style="color: #339933;">                startTimer = getTimer();</span>
<span style="color: #339933;">                timer.start();</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function fileRef_complete(evt:Event):void {</span>
<span style="color: #339933;">                Alert.show(evt.toString(), evt.type);</span>
<span style="color: #339933;">                timer.stop();</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function fileRef_ioError(evt:IOErrorEvent):void {</span>
<span style="color: #339933;">                Alert.show(evt.text, evt.type);</span>
<span style="color: #339933;">                timer.stop();</span>
<span style="color: #339933;">            }</span>
<span style="color: #339933;">        ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> label=<span style="color: #ff0000;">&quot;upload&quot;</span> click=<span style="color: #ff0000;">&quot;start();&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> id=<span style="color: #ff0000;">&quot;lbl&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The previous example just uploads to http://localhost/ (in my case my local JRun server). Since that is pretty much useless 99.8% of the time, you would need to change that URL to your ColdFusion/PHP/ASP/Java upload script on your own server.</p>
<p>If you&#8217;re a ColdFusion&#8217;er, here&#8217;s the script that I use to upload files:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfsilent</span><span style="color: #0000FF;">&gt;</span></span><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfsetting</span> enablecfoutputonly<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> req <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">getHTTPRequestData</span><span style="color: #0000FF;">&#40;</span> <span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;UPLOAD&quot;</span> filefield<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;Filedata&quot;</span> destination<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#ExpandPath('.')#&quot;</span> nameconflict<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;MAKEUNIQUE&quot;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfsavecontent</span> variable<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;info&quot;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #000000; font-weight: bold;">html</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #000000; font-weight: bold;">head</span><span style="color: #0000FF;">&gt;</span></span><span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #000000; font-weight: bold;">head</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #000000; font-weight: bold;">body</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #0000FF;">label</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;CFFILE&quot;</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#cffile#&quot;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #0000FF;">label</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;getHTTPRequestData()&quot;</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#req#&quot;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">IsDefined</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;FORM&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #0000FF;">label</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;FORM&quot;</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#FORM#&quot;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">IsDefined</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;URL&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #0000FF;">label</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;URL&quot;</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#URL#&quot;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #000000; font-weight: bold;">body</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #000000; font-weight: bold;">html</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfsavecontent</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;WRITE&quot;</span> file<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#ExpandPath('./')##cffile.serverFileName#.dump.html&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#info#&quot;</span> addnewline<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;Yes&quot;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfsilent</span><span style="color: #0000FF;">&gt;</span></span><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfsetting</span> enablecfoutputonly<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfcontent</span> reset<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfoutput</span><span style="color: #0000FF;">&gt;</span></span>fileName=<span style="color: #0000FF;">#CFFILE.serverFile#</span>&amp;fileSize=<span style="color: #0000FF;">#CFFILE.fileSize#</span><span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfoutput</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>It&#8217;s a pretty handy script as it not only uploads the file to the server (which is kind of the point), but it also creates a separate HTML file for each upload which tells me things like file names, file size, content length, content type, user agent (&#8220;Shockwave Flash&#8221;, if you&#8217;re curious as to what Flash Player uses for a user agent). It even correctly logs the parameters I passed from Flex to ColdFusion using URLVariables (userID and fpVersion).</p>
<p class="new">For an example of sending variables from a server-side script back to your Flex application after a file upload, see <a href="http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/">&#8220;Using for the FileReference class’s uploadCompleteData event to capture data from a server-side script&#8221;</a>.</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Using the URLVariables and FileReference classes to pass data from Flex to a server-side script on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/',contentID: 'post-259',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'getTimer(),upload()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/10/30/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Uploading files in Flex using the FileReference class</title>
		<link>http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/</link>
		<comments>http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/#comments</comments>
		<pubDate>Sat, 22 Sep 2007 03:28:38 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[FileReference]]></category>
		<category><![CDATA[browse()]]></category>
		<category><![CDATA[upload()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/</guid>
		<description><![CDATA[<p>In an earlier example we looked at &#8220;<a href="http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/">Downloading files in Flex using the FileReference class</a>&#8220;, but It looks like we haven&#8217;t ever looked at file uploads.<br /> The following example shows how you can use the FileReference class&#8217;s browse() method to allow users to select and upload a single file to a Web server.</p> [...]]]></description>
			<content:encoded><![CDATA[<p>In an earlier example we looked at &#8220;<a href="http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/">Downloading files in Flex using the FileReference class</a>&#8220;, but It looks like we haven&#8217;t ever looked at file uploads.<br />
The following example shows how you can use the FileReference class&#8217;s <code>browse()</code> method to allow users to select and upload a single file to a Web server.</p>
<p class="note">If you want to allow users to upload multiple files at once, you would use the FileReferenceList class instead of FileReference.</p>
<p>Full code after the jump.</p>
<p><span id="more-202"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_browse_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!-- http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/ --&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
        layout=&quot;vertical&quot;
        verticalAlign=&quot;middle&quot;
        backgroundColor=&quot;white&quot;
        creationComplete=&quot;init();&quot;&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private var fileRef:FileReference;

            private const FILE_UPLOAD_URL:String = <strong style="color:red;">&quot;http://www.YOUR-WEBSITE-HERE.com/fileref/uploader.cfm&quot;</strong>;

            private function init():void {
                fileRef = new FileReference();
                fileRef.addEventListener(Event.SELECT, fileRef_select);
                fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
                fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
            }

            private function browseAndUpload():void {
                fileRef.browse();
                message.text = &quot;&quot;;
            }

            private function fileRef_select(evt:Event):void {
                try {
                    message.text = &quot;size (bytes): &quot; + numberFormatter.format(fileRef.size);
                    fileRef.upload(new URLRequest(FILE_UPLOAD_URL));
                } catch (err:Error) {
                    message.text = &quot;ERROR: zero-byte file&quot;;
                }
            }

            private function fileRef_progress(evt:ProgressEvent):void {
                progressBar.visible = true;
            }

            private function fileRef_complete(evt:Event):void {
                message.text += &quot; (complete)&quot;;
                progressBar.visible = false;
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:NumberFormatter id=&quot;numberFormatter&quot; /&gt;

    &lt;mx:Button label=&quot;Upload file&quot;
            click=&quot;browseAndUpload();&quot; /&gt;
    &lt;mx:Label id=&quot;message&quot; /&gt;
    &lt;mx:ProgressBar id=&quot;progressBar&quot;
            indeterminate=&quot;true&quot;
            visible=&quot;false&quot; /&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_browse_test/bin/srcview/index.html">View source</a></p>
<p class="alert">You&#8217;ll notice the previous example doesn&#8217;t have a SWF or file upload URL. That would be because I don&#8217;t want all of you uploading files to my server. :)</p>
<p class="construction">In the previous example, I wrapped the call to <code>fileRef.size</code> in a try..catch block. In ActionScript 3.0, Flash Player throws an error if you attempt to get the size of a zero-byte file.</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Uploading files in Flex using the FileReference class on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/',contentID: 'post-202',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'browse(),upload()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/feed/</wfw:commentRss>
		<slash:comments>145</slash:comments>
		</item>
		<item>
		<title>Downloading files in Flex using the FileReference class</title>
		<link>http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/</link>
		<comments>http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/#comments</comments>
		<pubDate>Sat, 28 Jul 2007 17:20:53 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[FileReference]]></category>
		<category><![CDATA[dataTipField]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[showDataTips]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/</guid>
		<description><![CDATA[<p>OK, enough embedding examples, lets take a look at downloading files using Flash Player&#8217;s FileReference class (flash.net.FileReference). This example demonstrates a basic usage of the FileReference class within Flex, allowing users to download a file from the server. This example also shows how you can use data tips in the DataGrid control by setting the [...]]]></description>
			<content:encoded><![CDATA[<p>OK, enough embedding examples, lets take a look at downloading files using Flash Player&#8217;s FileReference class (flash.net.FileReference). This example demonstrates a basic usage of the FileReference class within Flex, allowing users to download a file from the server. This example also shows how you can use data tips in the DataGrid control by setting the data grid column&#8217;s <code>showDataTips</code> property to <code>true</code> and specifying a value for the <code>dataTipField</code> column.</p>
<p>Full code after the jump.</p>
<p><span id="more-35"></span></p>
<p>The following example downloads a ZIP file when the user presses the Button control. You can mouse over the items in the DataGrid control&#8217;s &#8220;Type&#8221; column to see additional event information.</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test/bin/srcview/FileReference_download_test.zip">Download source (ZIP, 3K)</a> | <a href="/wp-content/uploads/FileReference_download_test/main.mxml">View MXML</a></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        layout=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #000000;">        verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">        backgroundColor=<span style="color: #ff0000;">&quot;white&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;init();&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">        &lt;![CDATA[</span>
<span style="color: #339933;">            import mx.controls.Alert;</span>
<span style="color: #339933;">            import mx.collections.ArrayCollection;</span>
<span style="color: #339933;">            import flash.net.FileReference;</span>
&nbsp;
<span style="color: #339933;">            [Bindable]</span>
<span style="color: #339933;">            [Embed('assets/disk.png')]</span>
<span style="color: #339933;">            private var diskIcon:Class;</span>
&nbsp;
<span style="color: #339933;">            [Bindable]</span>
<span style="color: #339933;">            private var arrColl:ArrayCollection;</span>
&nbsp;
<span style="color: #339933;">            /* URL of the file to download. */</span>
<span style="color: #339933;">            private const FILE_URL:String = &quot;http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test/bin/srcview/FileReference_download_test.zip&quot;;</span>
&nbsp;
<span style="color: #339933;">            private var fileRef:FileReference;</span>
<span style="color: #339933;">            private var urlReq:URLRequest;</span>
&nbsp;
<span style="color: #339933;">            private function init():void {</span>
<span style="color: #339933;">                /* Initialize the array collection to an empty collection. */</span>
<span style="color: #339933;">                arrColl = new ArrayCollection();</span>
&nbsp;
<span style="color: #339933;">                /* Set up the URL request to download the file specified by the FILE_URL variable. */</span>
<span style="color: #339933;">                urlReq = new URLRequest(FILE_URL);</span>
&nbsp;
<span style="color: #339933;">                /* Define file reference object and add a bunch of event listeners. */</span>
<span style="color: #339933;">                fileRef = new FileReference();</span>
<span style="color: #339933;">                fileRef.addEventListener(Event.CANCEL, doEvent);</span>
<span style="color: #339933;">                fileRef.addEventListener(Event.COMPLETE, doEvent);</span>
<span style="color: #339933;">                fileRef.addEventListener(Event.OPEN, doEvent);</span>
<span style="color: #339933;">                fileRef.addEventListener(Event.SELECT, doEvent);</span>
<span style="color: #339933;">                fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent);</span>
<span style="color: #339933;">                fileRef.addEventListener(IOErrorEvent.IO_ERROR, doEvent);</span>
<span style="color: #339933;">                fileRef.addEventListener(ProgressEvent.PROGRESS, doEvent);</span>
<span style="color: #339933;">                fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function doEvent(evt:Event):void {</span>
<span style="color: #339933;">                /* Create shortcut to the FileReference object. */</span>
<span style="color: #339933;">                var fr:FileReference = evt.currentTarget as FileReference;</span>
&nbsp;
<span style="color: #339933;">                /* Add event order and type to the DataGrid control. */</span>
<span style="color: #339933;">                arrColl.addItem({data:arrColl.length+1, type:evt.type, eventString:evt.toString()});</span>
&nbsp;
<span style="color: #339933;">                try {</span>
<span style="color: #339933;">                    /* Update the Model. */</span>
<span style="color: #339933;">                    fileRefModel.creationDate = fr.creationDate;</span>
<span style="color: #339933;">                    fileRefModel.creator = fr.creator;</span>
<span style="color: #339933;">                    fileRefModel.modificationDate = fr.modificationDate;</span>
<span style="color: #339933;">                    fileRefModel.name = fr.name;</span>
<span style="color: #339933;">                    fileRefModel.size = fr.size;</span>
<span style="color: #339933;">                    fileRefModel.type = fr.type;</span>
<span style="color: #339933;">                    /* Display the Text control. */</span>
<span style="color: #339933;">                    txt.visible = true;</span>
<span style="color: #339933;">                } catch (err:*) {</span>
<span style="color: #339933;">                    /* uh oh, an error of sorts. */</span>
<span style="color: #339933;">                }</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function downloadSourceCodeZip():void {</span>
<span style="color: #339933;">                /* Clear existing array collection. */</span>
<span style="color: #339933;">                arrColl = new ArrayCollection();</span>
<span style="color: #339933;">                /* Hide the Text control. */</span>
<span style="color: #339933;">                txt.visible = false;</span>
<span style="color: #339933;">                /* Begin download. */</span>
<span style="color: #339933;">                fileRef.download(urlReq);</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function showAlert(item:Object):void {</span>
<span style="color: #339933;">                Alert.show(item.eventString, item.type);</span>
<span style="color: #339933;">            }</span>
<span style="color: #339933;">        ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Model</span> id=<span style="color: #ff0000;">&quot;fileRefModel&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;file<span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;">&lt;creationDate<span style="color: #7400FF;">&gt;</span></span>{&quot;&quot;}<span style="color: #000000;">&lt;/creationDate<span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;">&lt;creator<span style="color: #7400FF;">&gt;</span></span>{&quot;&quot;}<span style="color: #000000;">&lt;/creator<span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;">&lt;modificationDate<span style="color: #7400FF;">&gt;</span></span>{&quot;&quot;}<span style="color: #000000;">&lt;/modificationDate<span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;">&lt;name<span style="color: #7400FF;">&gt;</span></span>{&quot;&quot;}<span style="color: #000000;">&lt;/name<span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;">&lt;size<span style="color: #7400FF;">&gt;</span></span>{&quot;&quot;}<span style="color: #000000;">&lt;/size<span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;">&lt;type<span style="color: #7400FF;">&gt;</span></span>{&quot;&quot;}<span style="color: #000000;">&lt;/type<span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;/file<span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Model</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> id=<span style="color: #ff0000;">&quot;downloadBtn&quot;</span></span>
<span style="color: #000000;">            label=<span style="color: #ff0000;">&quot;Download example source code&quot;</span></span>
<span style="color: #000000;">            icon=<span style="color: #ff0000;">&quot;{diskIcon}&quot;</span></span>
<span style="color: #000000;">            click=<span style="color: #ff0000;">&quot;downloadSourceCodeZip();&quot;</span></span>
<span style="color: #000000;">            toolTip=<span style="color: #ff0000;">&quot;{FILE_URL}&quot;</span></span>
<span style="color: #000000;">            height=<span style="color: #ff0000;">&quot;40&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGrid</span> id=<span style="color: #ff0000;">&quot;debug&quot;</span></span>
<span style="color: #000000;">            dataProvider=<span style="color: #ff0000;">&quot;{arrColl}&quot;</span></span>
<span style="color: #000000;">            width=<span style="color: #ff0000;">&quot;{downloadBtn.width}&quot;</span></span>
<span style="color: #000000;">            rowCount=<span style="color: #ff0000;">&quot;5&quot;</span></span>
<span style="color: #000000;">            rowHeight=<span style="color: #ff0000;">&quot;22&quot;</span></span>
<span style="color: #000000;">            itemClick=<span style="color: #ff0000;">&quot;showAlert(event.currentTarget.selectedItem)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> dataField=<span style="color: #ff0000;">&quot;data&quot;</span></span>
<span style="color: #000000;">                    headerText=<span style="color: #ff0000;">&quot;#&quot;</span></span>
<span style="color: #000000;">                    width=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> dataField=<span style="color: #ff0000;">&quot;type&quot;</span></span>
<span style="color: #000000;">                    headerText=<span style="color: #ff0000;">&quot;Type&quot;</span></span>
<span style="color: #000000;">                    showDataTips=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">                    dataTipField=<span style="color: #ff0000;">&quot;eventString&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:DataGrid</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Text</span> id=<span style="color: #ff0000;">&quot;txt&quot;</span> condenseWhite=<span style="color: #ff0000;">&quot;true&quot;</span> visible=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:text</span><span style="color: #7400FF;">&gt;</span></span>
            creationDate: {fileRefModel.creationDate}
            creator: {fileRefModel.creator}
            modificationDate: {fileRefModel.modificationDate}
            name: {fileRefModel.name}
            size: {fileRefModel.size}
            type: {fileRefModel.type}
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:text</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Text</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="information">View source enabled in the following example.</p>
<p><iframe height="400" width="100%" src="http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test/bin/main.html"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Downloading files in Flex using the FileReference class on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/',contentID: 'post-35',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'dataTipField,download,showDataTips',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
	</channel>
</rss>

