Flex中利用URLVariables和FileReference类Flex向服务器端脚本传送数据的例子

By Minidxer | November 1, 2008

接下来的例子演示了Flex中利用URLVariables和FileReference类,Flex向服务器端脚本传送数据。

由于服务器不支持ColdFusion,所以没办法创建演示Demo,有兴趣地可以自己编译一个试试看。


Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white"
  6.         creationComplete="init();">
  7.  
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import flash.net.FileReference;
  11.             import flash.net.URLRequestMethod;
  12.             import mx.controls.Alert;
  13.             import mx.utils.StringUtil;
  14.  
  15.             private var fileRef:FileReference;
  16.             private var urlVars:URLVariables;
  17.             private var urlReq:URLRequest;
  18.             private var startTimer:Number;
  19.             private var timer:Timer;
  20.  
  21.             private function init():void {
  22.                 fileRef = new FileReference();
  23.                 fileRef.addEventListener(Event.SELECT, fileRef_select);
  24.                 fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
  25.                 fileRef.addEventListener(IOErrorEvent.IO_ERROR, fileRef_ioError);
  26.  
  27.                 urlVars = new URLVariables();
  28.                 urlVars.userID = 94103;
  29.                 urlVars.fpVersion = flash.system.Capabilities.version;
  30.  
  31.                 urlReq = new URLRequest();
  32.                 urlReq.method = URLRequestMethod.POST;
  33.                 urlReq.data = urlVars;
  34.                 urlReq.url = "http://localhost:8300/fileref/uploader.cfm";
  35.  
  36.                 timer = new Timer(100);
  37.                 timer.addEventListener(TimerEvent.TIMER, onTimer);
  38.             }
  39.  
  40.             private function onTimer(evt:TimerEvent):void {
  41.                 lbl.text = String(getTimer() - startTimer) + " ms";
  42.             }
  43.  
  44.             private function start():void {
  45.                 fileRef.browse();
  46.             }
  47.  
  48.             private function fileRef_select(evt:Event):void {
  49.                 fileRef.upload(urlReq);
  50.                 startTimer = getTimer();
  51.                 timer.start();
  52.             }
  53.  
  54.             private function fileRef_complete(evt:Event):void {
  55.                 Alert.show(evt.toString(), evt.type);
  56.                 timer.stop();
  57.             }
  58.  
  59.             private function fileRef_ioError(evt:IOErrorEvent):void {
  60.                 Alert.show(evt.text, evt.type);
  61.                 timer.stop();
  62.             }
  63.         ]]>
  64.     </mx:Script>
  65.  
  66.     <mx:Button label="upload" click="start();" />
  67.     <mx:Label id="lbl" />
  68.  
  69. </mx:Application>

下面是ColdFusion代码:

  1. <cfsilent><cfsetting enablecfoutputonly="true" />
  2. <cfset req = getHTTPRequestData( )>
  3.  
  4. <cffile action="UPLOAD" filefield="Filedata" destination="#ExpandPath('.')#" nameconflict="MAKEUNIQUE">
  5. <cfsavecontent variable="info">
  6. <html>
  7. <head></head>
  8. <body>
  9. <cfdump label="CFFILE" var="#cffile#">
  10. <cfdump label="getHTTPRequestData()" var="#req#">
  11. <cfif IsDefined("FORM")>
  12.     <cfdump label="FORM" var="#FORM#">
  13. </cfif>
  14. <cfif IsDefined("URL")>
  15.     <cfdump label="URL" var="#URL#">
  16. </cfif>
  17. </body>
  18. </html>
  19. </cfsavecontent>
  20.  
  21. <cffile action="WRITE" file="#ExpandPath('./')##cffile.serverFileName#.dump.html" output="#info#" addnewline="Yes">
  22. </cfsilent><cfsetting enablecfoutputonly="false" />
  23. <cfcontent reset="true" />
  24. <cfoutput>fileName=#CFFILE.serverFile#&fileSize=#CFFILE.fileSize#</cfoutput>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: FileReference | 1 Comment » | Tags: , , , ,

Search Posts