我的知识库

知识等于力量

« 应用软件系统架构设计的“七种武器”linux下Subversion服务架设 »

Flex2和PHP交互上传文件

  1. -------------------------------------------------------------Flex---------------------------------------------------
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init();" width="200" height="150">  
  4.     <mx:Script>  
  5.         <![CDATA[  
  6.             import flash.net.FileReference;  
  7.             import mx.controls.Alert;  
  8.             import mx.events.CloseEvent;  
  9.             import flash.events.*;  
  10.                           
  11.             private var file : FileReference;  
  12.             private var uploadURL : URLRequest;  
  13.               
  14.             private function init() : void{  
  15.                 Security.allowDomain("*");   
  16.                 file = new FileReference();   
  17.                 file.addEventListener(ProgressEvent.PROGRESS, onProgress);   
  18.                 file.addEventListener(Event.SELECT, onSelect);  
  19.                 uploadURL = new URLRequest();   
  20.                 uploadURL.url = "http://localhost:8081/upload.php";   
  21.             }  
  22.               
  23.             private function upload() : void{  
  24.                 var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");  
  25.                 var allTypes:Array = new Array(imageTypes);  
  26.                 file.browse(allTypes);  
  27.             }  
  28.               
  29.             private function onSelect(e : Event) : void{  
  30.                 Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?",    "确认上传",Alert.YES|Alert.NO,null,proceedWithUpload);  
  31.             }  
  32.               
  33.             private function onProgress(e:ProgressEvent) : void{  
  34.                 lbProgress.text = " 已上传 " + e.bytesLoaded + " 字节,共 " + e.bytesTotal + " 字节";  
  35.             }  
  36.               
  37.             private function proceedWithUpload(e : CloseEvent) : void{  
  38.                 if (e.detail == Alert.YES){  
  39.                     file.upload(uploadURL);  
  40.                 }  
  41.             }  
  42.         ]]>  
  43.     </mx:Script>  
  44.        
  45.     <mx:Canvas width="100%" height="100%">  
  46.         <mx:VBox width="100%" horizontalAlign="center">  
  47.         </mx:VBox>  
  48.         <mx:Button label="上传文件" click="upload();" y="91"/>  
  49.         <mx:Label id="lbProgress" text="上传" x="87.5" y="26"/>  
  50.     </mx:Canvas>  
  51.  
  52. </mx:Application>
  53. --------------------------------------------------HTML-----------------------------------------------------------------------------------
  54. <!-- saved from url=(0014)about:internet -->  
  55. <html lang="en">  
  56. <head>  
  57. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  58. <title>upload</title>  
  59. <body scroll="no">  
  60.   
  61.     <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"  
  62.             id="fileupload" width="200" height="150"  
  63.             codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">  
  64.             <param name="movie" value="fileupload.swf" />  
  65.             <param name="quality" value="high" />  
  66.             <param name="bgcolor" value="#869ca7" />  
  67.             <param name="allowScriptAccess" value="sameDomain" />  
  68.             <embed src="fileupload.swf" quality="high" bgcolor="#869ca7"  
  69.                 width="200" height="150" name="fileupload" align="middle"  
  70.                 play="true"  
  71.                 loop="false"  
  72.                 quality="high"  
  73.                 allowScriptAccess="sameDomain"  
  74.                 type="application/x-shockwave-flash"  
  75.                 pluginspage="http://www.adobe.com/go/getflashplayer">  
  76.             </embed>  
  77.     </object>  
  78. --------------------------------------------------PHP----------------------------------------------------------------------------
  79. </body>  
  80. </html>  
  81. <?php     
  82. // Flash 传递的文件表单 name 属性为 Filedata     
  83. $fileName = $_FILES["Filedata"]["name"];     
  84. $file = $_FILES["Filedata"]["tmp_name"];     
  85. $path = "uploadFiles/";     
  86. if (move_uploaded_file($file, $path . $fileName)){     
  87.  echo 1;     
  88. }else{     
  89.  echo 0;     
  90. }   
  91. ?>

Search

导航

热门文章

最新文章

Powered By duduwolf's wiki 1.0

Copyright 1999-2007 duduwolf.com Some Rights Reserved.