[英]Flex Download a file using AMFPHP
有誰知道是否可以使用AMFPHP下載文件。我發現了一些直接從php下載的示例。 如果是,請舉一些例子? 我有一個例程可以使用AMFPHP上傳文件,但是我沒有在網上找到要使用AMFPHP下載的示例。
提前致謝。
嘗試使用ByteArray。 類似於返回新的Amfphp_Core_Amf_Types_ByteArray($ data);
我找到了這篇文章: http : //gertonscorner.wordpress.com/2009/03/15/fileupload-using-amfphp-remoteobject-and-flash-10/ 。 在此基礎上,我創建了自己的版本,因為我的服務是通過service-config.xml按照amfphp文檔的“使用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 Project的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>
對我來說,它有效! 至少在我的開發服務器上。 請注意此處的安全漏洞。 您需要提供某種形式的保護,以防止任何濫用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.