<?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; upload()</title>
	<atom:link href="http://blog.flexexamples.com/tag/upload/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>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>
	</channel>
</rss>

