2012-12-10 45 views
0

有誰知道是否可以使用AMFPHP下載文件。我發現了一些使用直接從php下載的例子。如果是的話,一些例子?我有一個使用AMFPHP上傳文件的例程,但是我沒有在網上找到使用AMFPHP下載的例子。Flex使用AMFPHP下載文件

在此先感謝。

回答

0

嘗試使用ByteArray。類似 返回新的Amfphp_Core_Amf_Types_ByteArray($ data);

0

我發現這篇文章:http://gertonscorner.wordpress.com/2009/03/15/fileupload-using-amfphp-remoteobject-and-flash-10/。在此基礎上,我創建了自己的版本,因爲我的服務是通過service-config.xml按照amfphp的文檔Your first Project using Amfphp中描述的方式定義的。

服務器端:

如果你已經知道如何配置網關,以達到既服務類和值對象類,這裏有兩類:

的ValueObject類:

<?php 
class FileVO { 

    public $filename; 
    public $filedata; 

    // explicit actionscript class 
    var $_explicitType = "remoting.vo.FileVO"; 
} 
?> 

服務類:

<?php 
class RemoteFile { 
/** 
* Upload files! 
* 
* @param FileVO $file 
* @return string 
**/ 
    public function upload(FileVO $file) { 
    $data = $file->filedata->data; 
    file_put_contents(UPLOAD_PATH . $file->filename, $data); 
    return 'File: ' . $file->filename .' Uploaded '; 
    } 
} 
?> 

你可能注意到了,我們有一個常量UPLOAD_PATH,它是我們放置文件的目錄。這個目錄中文件的名稱將是它來自客戶端的名稱。當然,你需要改變這一點,但現在已經超出了這個例子的範圍。

客戶端:

在客戶端,在Flex項目的src文件夾我建立與值對象類包,保存爲ActionScrip文件:

package remoting.vo 
{ 
import flash.utils.ByteArray; 

    [RemoteClass(alias="FileVO")] 
    public class FileVO{ 
    public var filename:String; 
    public var filedata:ByteArray; 

    public function FileVO(){ 
    } 

    } 
} 

的組分與文件上傳:

<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" 
     xmlns:net="flash.net.*"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
     <net:FileReference id="fileReference" 
          select="select_handler(event)" 
          complete="complete_handler(event);" 
          /> 
     <s:RemoteObject id="ro" 
         destination="amfphp" 
         source="RemoteFile" 
         result="handleResult(event)" 
         fault="Alert.show('Error ! ')"/> 
    </fx:Declarations> 

    <fx:Script> 
     <![CDATA[ 
      import mx.controls.Alert; 
      import mx.rpc.events.ResultEvent; 

      import remoting.vo.FileVO; 

      public function handleResult(event:ResultEvent):void{ 
       Alert.show('the server said : ' + event.result); 
      } 

      private function btn_click(evt:MouseEvent):void { 
       fileReference.browse(); 
      } 
      private function select_handler(evt:Event):void{ 
       fileReference.load(); 
      } 
      private function complete_handler(evt:Event):void{ 
       var data:ByteArray = new ByteArray(); 
       var file:FileVO; 

       //Read the bytes into bytearray var 
       fileReference.data.readBytes(data, 0, fileReference.data.length); 

       // Create a new FileVO instance 
       file = new FileVO(); 
       file.filename = fileReference.name; 
       file.filedata = data; 
       ro.upload(file); 
      } 

      private function onEvent(evt:Event):void { 
      Alert.show(evt.toString(), evt.type); 
      } 
     ]]> 
    </fx:Script> 
    <s:Button id="btn" label="Browse to Upload" click="btn_click(event);" /> 
</s:Group> 

對我來說它的工作原理!至少在我的開發服務器上。請注意這裏的安全漏洞。您需要提供某種形式的保護以防止濫用。