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. import flash.net.FileReference;
  9. import flash.net.URLRequestMethod;
  10. import mx.controls.Alert;
  11. import mx.utils.StringUtil;
  12. private var fileRef:FileReference;
  13. private var urlVars:URLVariables;
  14. private var urlReq:URLRequest;
  15. private var startTimer:Number;
  16. private var timer:Timer;
  17. private function init():void {
  18. fileRef = new FileReference();
  19. fileRef.addEventListener(Event.SELECT, fileRef_select);
  20. fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
  21. fileRef.addEventListener(IOErrorEvent.IO_ERROR, fileRef_ioError);
  22. urlVars = new URLVariables();
  23. urlVars.userID = 94103;
  24. urlVars.fpVersion = flash.system.Capabilities.version;
  25. urlReq = new URLRequest();
  26. urlReq.method = URLRequestMethod.POST;
  27. urlReq.data = urlVars;
  28. urlReq.url = "http://localhost:8300/fileref/uploader.cfm";
  29. timer = new Timer(100);
  30. timer.addEventListener(TimerEvent.TIMER, onTimer);
  31. }
  32. private function onTimer(evt:TimerEvent):void {
  33. lbl.text = String(getTimer() - startTimer) + " ms";
  34. }
  35. private function start():void {
  36. fileRef.browse();
  37. }
  38. private function fileRef_select(evt:Event):void {
  39. fileRef.upload(urlReq);
  40. startTimer = getTimer();
  41. timer.start();
  42. }
  43. private function fileRef_complete(evt:Event):void {
  44. Alert.show(evt.toString(), evt.type);
  45. timer.stop();
  46. }
  47. private function fileRef_ioError(evt:IOErrorEvent):void {
  48. Alert.show(evt.text, evt.type);
  49. timer.stop();
  50. }
  51. ]]>
  52. mx:Script>
  53. <mx:Buttonlabel="upload" click="start();" />
  54. <mx:Labelid="lbl" />
  55. 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,ColdFusion,Adobe)