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

来源:互联网 发布:adobe cc mac 激活工具 编辑:程序博客网 时间:2024/05/16 00:58
 
  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.     <mx:Script>
  8.         <![CDATA[
  9.             import flash.net.FileReference;
  10.             import flash.net.URLRequestMethod;
  11.             import mx.controls.Alert;
  12.             import mx.utils.StringUtil;
  13.             private var fileRef:FileReference;
  14.             private var urlVars:URLVariables;
  15.             private var urlReq:URLRequest;
  16.             private var startTimer:Number;
  17.             private var timer:Timer;
  18.             private function init():void {
  19.                 fileRef = new FileReference();
  20.                 fileRef.addEventListener(Event.SELECT, fileRef_select);
  21.                 fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
  22.                 fileRef.addEventListener(IOErrorEvent.IO_ERROR, fileRef_ioError);
  23.                 urlVars = new URLVariables();
  24.                 urlVars.userID = 94103;
  25.                 urlVars.fpVersion = flash.system.Capabilities.version;
  26.                 urlReq = new URLRequest();
  27.                 urlReq.method = URLRequestMethod.POST;
  28.                 urlReq.data = urlVars;
  29.                 urlReq.url = "http://localhost:8300/fileref/uploader.cfm";
  30.                 timer = new Timer(100);
  31.                 timer.addEventListener(TimerEvent.TIMER, onTimer);
  32.             }
  33.             private function onTimer(evt:TimerEvent):void {
  34.                 lbl.text = String(getTimer() - startTimer) + " ms";
  35.             }
  36.             private function start():void {
  37.                 fileRef.browse();
  38.             }
  39.             private function fileRef_select(evt:Event):void {
  40.                 fileRef.upload(urlReq);
  41.                 startTimer = getTimer();
  42.                 timer.start();
  43.             }
  44.             private function fileRef_complete(evt:Event):void {
  45.                 Alert.show(evt.toString(), evt.type);
  46.                 timer.stop();
  47.             }
  48.             private function fileRef_ioError(evt:IOErrorEvent):void {
  49.                 Alert.show(evt.text, evt.type);
  50.                 timer.stop();
  51.             }
  52.         ]]>
  53.     </mx:Script>
  54.     <mx:Button label="upload" click="start();" />
  55.     <mx:Label id="lbl" />
  56. </mx:Application>

下面是ColdFusion代码:

  1. <cfsilent><cfsetting enablecfoutputonly="true" />
  2. <cfset req = getHTTPRequestData( )>
  3. <cffile action="UPLOAD" filefield="Filedata" destination="#ExpandPath('.')#" nameconflict="MAKEUNIQUE">
  4. <cfsavecontent variable="info">
  5. <html>
  6. <head></head>
  7. <body>
  8. <cfdump label="CFFILE" var="#cffile#">
  9. <cfdump label="getHTTPRequestData()" var="#req#">
  10. <cfif IsDefined("FORM")>
  11.     <cfdump label="FORM" var="#FORM#">
  12. </cfif>
  13. <cfif IsDefined("URL")>
  14.     <cfdump label="URL" var="#URL#">
  15. </cfif>
  16. </body>
  17. </html>
  18. </cfsavecontent>
  19. <cffile action="WRITE" file="#ExpandPath('./')##cffile.serverFileName#.dump.html" output="#info#" addnewline="Yes">
  20. </cfsilent><cfsetting enablecfoutputonly="false" />
  21. <cfcontent reset="true" />
  22. <cfoutput>fileName=#CFFILE.serverFile#&fileSize=#CFFILE.fileSize#</cfoutput>
原创粉丝点击