有没有人知道是否可以使用AMFPHP.I下载文件。我找到了一些使用直接从php下载的示例。如果是,举个例子?我有一个使用AMFPHP上传文件的例程,但我在web上找不到使用AMFPHP下载的示例。
提前谢谢。
发布于 2012-12-12 04:10:10
尝试使用ByteArray。类似于返回新的Amfphp_Core_Amf_Types_ByteArray($data);
发布于 2014-02-01 07:01:16
我找到了这篇文章: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 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>对我来说,它是有效的!至少在我的开发服务器上。注意这里的安全漏洞。您需要提供某种形式的保护,以防止任何滥用。
https://stackoverflow.com/questions/13805073
复制相似问题