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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Applicationxmlns: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:Buttonlabel="upload" click="start();" />
  55. <mx:Labelid="lbl" />
  56. </mx:Application>

下面是ColdFusion代码:

  1. <cfsilent><cfsetting enablecfoutputonly="true" />
  2. <cfsetreq = getHTTPRequestData( )>
  3. <cffileaction="UPLOAD" filefield="Filedata" destination="#ExpandPath('.')#" nameconflict="MAKEUNIQUE">
  4. <cfsavecontentvariable="info">
  5. <html>
  6. <head></head>
  7. <body>
  8. <cfdumplabel="CFFILE" var="#cffile#">
  9. <cfdumplabel="getHTTPRequestData()" var="#req#">
  10. <cfifIsDefined("FORM")>
  11. <cfdumplabel="FORM" var="#FORM#">
  12. </cfif>
  13. <cfifIsDefined("URL")>
  14. <cfdumplabel="URL" var="#URL#">
  15. </cfif>
  16. </body>
  17. </html>
  18. </cfsavecontent>
  19. <cffileaction="WRITE" file="#ExpandPath('./')##cffile.serverFileName#.dump.html" output="#info#" addnewline="Yes">
  20. </cfsilent><cfsettingenablecfoutputonly="false" />
  21. <cfcontentreset="true" />
  22. <cfoutput>fileName=#CFFILE.serverFile#&fileSize=#CFFILE.fileSize#</cfoutput>

你可能感兴趣的:(Flex,脚本,Flash,Adobe,ColdFusion)