Fluid idea for Life

随笔,杂记

Flex上传文件例子 -- 《Flex第一步》知识点纠正

在《Flex第一步》的第十五章中,有一节关于文件上传的内容,这其中存在一个严重的知识性错误,在此特别作说明。

文中提到:在上传文件时,当上传结束后无法获取服务器的返回信息。最近翻看AS3.0的官方文档,发现是可以实现的。

FileReference对象的uploadCompleteData 事件在上载成功且从服务器接收数据之后被派发。 如果没有返回数据,则不派发此事件。我们只要侦听FileReference的uploadCompleteData事件,就可以获取返回数据。

下面是一个具体的实例:

<p><mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontsize="12" creationcomplete="init()" width="518" height="347"></p>
<p>        <mx:script>
         <!--[CDATA[</p>
<p>             import flash.net.FileReference;
            import flash.net.URLRequest;
            import flash.net.FileFilter;
            import flash.net.URLVariables;</p>
<p>            import flash.events.ProgressEvent;</p>
<p>            import mx.managers.CursorManager;
             //
             private var file:FileReference;</p>
<p>             internal function init():void{</p>
<p>                 file = new FileReference();
                 file.addEventListener(Event.SELECT, onSelect);
                file.addEventListener(ProgressEvent.PROGRESS, processHandler);
                //侦听UPLOAD_COMPLETE_DATA事件
                file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadDataHandler);</p>
<p>             }</p>
<p>             internal function doSelect():void{
                 var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.png)", "*.jpg;*.jpeg;*.png");
                var allTypes:Array = new Array(imageTypes);
                 file.browse(allTypes);
             }
             internal function onSelect(evt:Event):void{
                 pic_txt.text = file.name;
             }
             internal function doUpload():void{
                 var request:URLRequest = new URLRequest("upload.php");</p>
<p>                file.upload(request);
                CursorManager.setBusyCursor();
             }
             internal function processHandler(evt:ProgressEvent):void{
                //
                //evt.bytesTotal;
             }
             internal function uploadDataHandler(evt:DataEvent):void{
                 CursorManager.removeBusyCursor();
                 //获取返回数据,也就是上传后的图片地址,显示出来
                 img.source = evt.data.toString();
             }
         ]]-->
     </mx:script></p>
<p>    <mx:image id="img" width="377" height="108" x="10" y="59">
    <mx:textinput id="pic_txt" width="150" x="109" y="10">
    <mx:button label="选择文件" click="doSelect()" x="10" y="10">
    <mx:button label="开始上传" click="doUpload()" x="295" y="10"></p>
<p></mx:button>
</mx:button></mx:textinput></mx:image></mx:application>

upload.php的代码如下:

    //$imgName = $_GET['filename'];</p>
<p>	$file = $_FILES["Filedata"]["tmp_name"];   </p>
<p>	$oldName = $_FILES["Filedata"]["name"];  </p>
<p>	$ext = substr($oldName, strrpos($oldName, ".")); </p>
<p>	$newName = "pics/".date("YmdHis").$ext;</p>
<p>	$result = move_uploaded_file($file, $newName);   </p>
<p>	if($result){
		echo $newName;
	}else{
		echo "error";
	}
?&gt;

点击看运行的例子:http://www.fluidea.cn/book/uploadEx/FlexUpload.html 右键选择”View Source“可以下载源代码。

Tags: Flex

上一篇: 最近的新闻集合
下一篇: 在Flex3 beta版中使用flex2的程序

访客评论

  1. #1 walktree 2008-01-21, 12:02 PM
    这里我遗漏了一个重要的说明,flash.events.DateEvent对象的UPLOAD_COMPLETE_DATA属性是FLEX2.0.1才有的新功能。2.01升级包的下载地址是: http://download.macromedia.com/pub/flex/flex_builder/flexbuilder2_201updater.exe。而且,必须是FlashPlayer9.0.28版本以上才有用,安装flex2后Flash Player9的版本为9.0.16.0,必须升级。
  2. #2 kktflex 2008-01-21, 8:38 AM
    请问,导入测试后,出现这样的错误,请高人指教:
    1119: Access of possibly undefined property UPLOAD_COMPLETE_DATA through a reference with static type Class.
    要怎么解决??
  3. #3 柳世杀神 2008-01-15, 4:59 PM
    呃~我只想知道,将这点错误的知识点从书上改掉,应该怎么写……
    例如前篇文章中的总汇我就是将用传统的文学编辑改的,而这么大一段……这么大一陀……难道要我全部抄下来夹张纸放书里去吗……

发表评论

评论内容:

点击获得Trackback地址