Flex文件上传

  今天花了一点时间研究了一下FLEX的文件上传,后台采用PHP进行处理。本文的代码是整合了网上
  查找到的一些代码,都是转载来转载去的,原文已经不可考,就不一一在这里列出,感谢前人的分享
  精神,向他们学习吧。
  1. 首先先介绍点基本知识,php端的全局变量$_FILES数组
  $_FILES['userfile']['name'] 客户端机器文件的原名称。
  $_FILES['userfile']['type'] 文件的 MIME 类型,需要浏览器提供该信息的支持,例如"image/gif"。
  $_FILES['userfile']['size'] 已上传文件的大小,单位为字节。
  $_FILES['userfile']['tmp_name']文件被上传后在服务端储存的临时文件名。
  $_FILES['userfile']['error'] 和该文件上传相关的错误代码。
  2. php文件上传大小设置
  file_uploads = on //是否允许系统支持文件上传
  ;upload_tmp_dir //临时文件的存储路径,如果不设置就是系统默认的路径
  upload_max_filesize = 2m //允许文件上传最大体积
  post_max_size = 2m //通过post方法给php时,php所能接受的最大数据容量
  max_execution_time = 30 //每个script所执行的最大时间
  memory_limit = 8m //每个script所能消耗的最大memory
  上面这些值都是php.ini的默认值,如果我们要传更大的文件,需要对当中的某些具体参数进行修改
  一般上传的文件的信息都是保存在了$_FILES数组中,我们先来看一下PHP端如何处理。我们知道客户端上传的
  文件保存在了系统默认的临时文件夹中,我们的目标就是要将临时文件夹中的文件拷贝到我们需要保存的地址当中去。
  我们先来看一下PHP端的代码,将一一做出解释: $file_size_max) { echo "对不起,你的文件大小大于规定的上传限制"; exit; } } if (file_exists($uploadfile) && $accept_overwrite) { Echo "存在相同的文件名"; exit; } $moved = move_uploaded_file($_FILES['Filedata']['tmp_name'] ,$uploadfile); if(empty($moved)) { echo"复制文件失败"; exit; } echo " file moved " . $moved . "\n"; $errorNo=$_FILES['upload_file']['error']; switch($errorNo){ case 0: Echo "上传成功"; break; case 1: Echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值."; break; case 2: Echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。"; break; case 3: Echo "文件只有部分被上传";break; case 4: Echo "没有文件被上传";break; } ?> PHP端的代码比较简单,对上传的文件生成了一个独一无二的文件名,并对文件大小,文件名唯一性进行了简单的判断,
  最后使用php的move_upload_flie函数来实现文件的移动。对于随即数,可以使用rand和mt_rand函数,据说mt_rand
  要比rand要快很多,有兴趣的同学可以自己写个测试程序测试一下。
  现在我们转到前端FLEX处理,Flex采用actionscript语言+xml语言。代码中有详细的注释,就不做详细说明了。 Array = new Array(imageTypes); var success:Boolean = fileRef.browse(allTypes); } catch (error:Error) { trace("Unable to browse for files."); textarea1.text = "Unable to browse for files."; } } // checks that the upload php file is where we think it is public function test():void { request = new URLRequest(uploadFile); navigateToURL(request,"_blank"); } // when a file is selected we upload the file to the php file upload script on the server public function selectHandler(event:Event):void { try { // upload file Alert.show("上传 " + fileRef.name + " (共 "+Math.round(fileRef.size)+" 字节)?", "确认上传", Alert.YES|Alert.NO, null, proceedWithUpload); //fileRef.upload(request); textarea1.text = "Uploading " + fileRef.name + "..."; } catch (error:Error) { // vague trace("Unable to upload file."); textarea1.text += "\nUnable to upload file."; } } private function proceedWithUpload(e: CloseEvent): void{ if (e.detail == Alert.YES){ request = new URLRequest(uploadFile); try { fileRef.upload(request); } catch (error:Error) { Alert.show("上传失败"); } } } // dispatched during file open. public function openHandler(event:Event):void { trace("File opened"); textarea1.text += "\nFile opened"; } // dispatched during file upload public function progressHandler(event:ProgressEvent):void { trace("File upload in progress (" + event.bytesLoaded + " of " + event.bytesTotal + ")"); textarea1.text += "\nFile upload in progress (" + event.bytesLoaded + " of " + event.bytesTotal + ")"; lbProgress.text = " 已上传 " + event.bytesLoaded + " 字节,共 " + event.bytesTotal + " 字节"; var proc: uint = event.bytesLoaded / event.bytesTotal * 100; bar.setProgress(proc, 100); bar.label= "当前进度: " + " " + proc + "%"; } // dispatched when the file has been given to the server script // this event does not receive a response from the server // use DataEvent.UPLOAD_COMPLETE_DATA event as shown in uploadCompleteHandler public function completeHandler(event:Event):void { trace("File uploaded"); textarea1.text += "\nFile uploaded"; Alert.show("恭喜你,上传成功"); } // dispatched when a file upload has completed // this event can contain a response from the server as opposed to the Event.COMPLETE event // the php upload file can send back information if we want it to // the event.data and event.text properties would contain a response if any public function uploadCompleteHandler(event:DataEvent):void { trace("Information about upload: \n" + String(event.text)); textarea1.text += "\nInformation about upload \n" + event.text as String; } // dispatched when an http error occurs public function httpErrorHandler(event:HTTPStatusEvent):void { trace("HTTP error occured " + event.status); textarea1.text += "\nHTTP error occured - " + event.status; } // dispatched when an http io error occurs public function httpIOErrorHandler(event:IOErrorEvent):void { trace("HTTP IO error occured - " + event.text); textarea1.text += "\nHTTP IO error occured - " + event.text; } // dispatched when an http io error occurs public function httpSecurityErrorHandler(event:SecurityErrorEvent) :void { trace("HTTP Security error occured - " + event.text); textarea1.text += "\nHTTP Security error occured - " + event.text; } ]]-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值