AS3上传文件

upload.as:

package 
{
 import flash.display.*;
 import flash.net.*;
 import flash.events.*;
 import flash.text.*;
 import flash.ui.*;
 import fl.controls.List;
 import fl.controls.ScrollBarDirection;
 import fl.controls.TileList;
 import fl.data.DataProvider;
 //网络应用
 import flash.net.URLRequest;
 import flash.net.URLRequestHeader;
    import flash.net.URLRequestMethod;

 public class Upload extends Sprite
 {
  var base_url:String = './';
  var upload_btn:Btn = new Btn;/////UPLOAD_BTN
  var pending_files_array:Array;
  var upload_details_array:Array;
  var upload_details_contain:TileList;
  var dp:Array;
  var frl:FileReferenceList = new FileReferenceList;
  var images_filter:FileFilter = new FileFilter("*.jpg, *.jpeg, *.gif, *.png", "*.jpg;*.jpeg;*.gif;*.png");
  var docs_filter:FileFilter = new FileFilter("*.txt, *.doc, *.docx, *.rtf", "*.txt;*.doc;*.docx;*.rtf");
  var zip_filter:FileFilter = new FileFilter("*.zip, *.rar", "*.zip;*.rar");
  var all_filter:FileFilter=new FileFilter("所有文件 *.*","*.*");
  //var header:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
  var uploadURL:URLRequest = new URLRequest(base_url + 'upload.php');
  //uploadURL.method = URLRequestMethod.POST;
  //uploadURL.requestHeaders.push(header);
  
  public function Upload()
  {
   ////隐藏默认菜单
   var contextmenu:ContextMenu = new ContextMenu;
   contextmenu.hideBuiltInItems();
   ////添加右键菜单
   var item:ContextMenuItem = new ContextMenuItem('&设计网');
   contextmenu.customItems.push(item);
   item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:ContextMenuEvent)
   {
    ///URL定向
    navigateToURL(new URLRequest(base_url));
   });
   stage.align = StageAlign.TOP_LEFT;//顶端对齐
   stage.scaleMode = StageScaleMode.NO_SCALE;/////缩放模式
   addChild(upload_btn);/////
   upload_btn.buttonMode = true;
   upload_btn.addEventListener(MouseEvent.CLICK, uploadhandler);
   frl.addEventListener(Event.SELECT, selectedhandler);
   frl.addEventListener(Event.CANCEL, cancelhandler);
   stage.addEventListener(Event.RESIZE, resizehandler);
   resizehandler(null);
   contextMenu = contextmenu;
  }
  private function resizehandler(e:Event):void
  {
   ////舞台居中
   upload_btn.x = Math.floor((stage.stageWidth - upload_btn.width) / 2) + .5;
   upload_btn.y = Math.floor((stage.stageHeight - upload_btn.height) / 2) + .5;
  }
  private function uploadhandler(e:MouseEvent):void
  {
   frl.browse([images_filter, docs_filter, zip_filter,all_filter]);
  }
  private function cancelhandler(e:Event):void
  {
   ////
   stage.focus = null;
  }
  private function selectedhandler(e:Event):void
  {
   removeChild(upload_btn);///删除
   upload_details_contain = new TileList;
   dp = new Array;
   addChild(upload_details_contain);
   pending_files_array = new Array();
   upload_details_array = new Array();
   var fileReferenceList:FileReferenceList = e.target as FileReferenceList;
   var fileList:Array = fileReferenceList.fileList;
   for (var i:uint = 0; i < fileList.length; i++) {
    addPendingFile(fileList[i]);
   }
  }
  private function addPendingFile(file:FileReference):void
  {
   pending_files_array.push(file);
   ////上传进度条
   var udi:UploadDetailInfo = new UploadDetailInfo;
   upload_details_array.push(udi);
   dp.push({source:udi});
   upload_details_contain.dataProvider = new DataProvider(dp);
   upload_details_contain.direction = ScrollBarDirection.VERTICAL;////垂直方向显示游
   upload_details_contain.setStyle("contentPadding", 5);
   upload_details_contain.columnWidth = udi.width;
   upload_details_contain.rowHeight = udi.height + 20;
   upload_details_contain.setSize(udi.width, stage.stageHeight);
   if(dp.length * upload_details_contain.rowHeight < upload_details_contain.height){
    upload_details_contain.setSize(udi.width, dp.length * upload_details_contain.rowHeight);
   }
   upload_details_contain.move((stage.stageWidth - upload_details_contain.width) / 2, (stage.stageHeight - upload_details_contain.height) / 2);
   file.addEventListener(Event.OPEN, openHandler);
   file.addEventListener(Event.COMPLETE, completeHandler);
   file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
   file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
   file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
   file.upload(uploadURL,"Filedata");
  }
  
  private function getIndex(file:FileReference):int
  {
   //////
   ////多个文件名列表
   ////返回文件数
   for (var i:int; i < pending_files_array.length; i++) {
    if (pending_files_array[i].name == file.name) {
     return i;
    }
   }
   return i;
  }
  //////当上传完毕时,清空相关内容
  private function removePendingFile(file:FileReference):void
  {
   
   for (var i:uint; i < pending_files_array.length; i++) {
    if (pending_files_array[i].name == file.name) {
     pending_files_array.splice(i, 1);
     upload_details_array.splice(i, 1);
     if (pending_files_array.length == 0) {
      doOnComplete();
     }
     
     return;
    }
   }
  }
  private function doOnComplete():void
  {
   /////
   removeChild(upload_details_contain);
   ////保存文件列表
   var dp:DataProvider = new DataProvider;
   ///FRL,fileReferenceList
   for(var i=0; i < frl.fileList.length; i++){
    var n:String = FileReference(frl.fileList[i]).name;///对于每一个文件的文件名
    var l:String = base_url + 'uploads/' + n;
    /////对应文件名,上传服务器路径
    dp.addItem({label: n, data:l});
   }
   /////
   var list:List = new List;
   list.dataProvider = dp;
   //居中
   list.setSize(stage.stageWidth, stage.stageHeight);
   //数组长度*行高
   if(dp.length * list.rowHeight < list.height){
    list.setSize(stage.stageWidth, dp.length * list.rowHeight);
   }
   ///居中
   list.move((stage.stageWidth - list.width) / 2, (stage.stageHeight - list.height) / 2);
   addChild(list);
   list.addEventListener(Event.CHANGE, handler);
   function handler(e:Event):void
   {
    var dl:FileReference = new FileReference;
    downloadFile();
    ///下载文件
    function downloadFile():void
    {
     dl.download(new URLRequest(list.selectedItem.data));
    }
   }
   ///定义文本格式
   var tf:TextFormat = new TextFormat;
   tf.size = 12;
   var success:TextField = new TextField;
   success.height = 1;
   success.border=true;
   success.backgroundColor=0xcccccc;
   success.autoSize = TextFieldAutoSize.LEFT;
   success.text = '我还要传';
   success.textColor = 0xffffff;
   success.setTextFormat(tf);
   success.x = 0;
   success.y = 0;
   success.selectable = false;
   success.addEventListener(MouseEvent.CLICK, clickhandler);
   
   addChild(success);
   ///重新上传
   function clickhandler(e:MouseEvent):void
   {
    removeChild(list);
    removeChild(e.target as TextField);
    addChild(upload_btn);
   }
   
  }
  private function openHandler(event:Event):void
  {
   //开始上传
   var file:FileReference = FileReference(event.target);
   UploadDetailInfo(upload_details_array[getIndex(file)]).ready(file.name);
  }
  private function completeHandler(event:Event):void
  {
   ////上传完成
   var file:FileReference = FileReference(event.target);
   UploadDetailInfo(upload_details_array[getIndex(file)]).complete(file.name);
   removePendingFile(file);
  }
  private function progressHandler(event:ProgressEvent):void
  {
   ////上传进度
   var file:FileReference = FileReference(event.target);
   ////还少于10%时
   if(event.bytesLoaded / event.bytesTotal < .1){
    UploadDetailInfo(upload_details_array[getIndex(file)]).ready(file.name);
   } else {
    //显示进度
    UploadDetailInfo(upload_details_array[getIndex(file)]).updateinfo(file.name, event.bytesLoaded, event.bytesTotal);
   }
  }
  private function ioErrorHandler(event:Event):void
  {
   ///IOERROR处理
  }
  private function securityErrorHandler(event:Event):void
  
  {
   ///////
  }
 }
}

upload.php

<?php
$uploaddir = './uploads/';
$filename = $_FILES['Filedata']['name'];
$tmpname=$_FILES['Filedata']['tmp_name'];
$fp=fopen("myfile.txt","w+");
fwrite($fp,$filename);
fwrite($fp,$tmpname);
fclose($fp);

$uploadfile = $uploaddir . $filename;
$uploadfile = iconv('utf-8', 'gb2312', $uploadfile);
move_uploaded_file($tmpname, $uploadfile);
?>

uploaddetaildata.as

package
{
 import flash.display.*;
 import flash.text.*;
 
 public class UploadDetailInfo extends Sprite
 {
  public function UploadDetailInfo()
  {
   ///////
   _txt.autoSize = TextFieldAutoSize.LEFT;
   _mc.scaleX = 0;
   ///设置遮罩
   _mc.mask = _mask;
  }
  public function ready(n):void
  {
   /////准备上传文件名
   _txt.text = '准备上传:' + n;
   _txt.textColor = 0x666666;
  }
  public function updateinfo(n,b,t):void
  
  {
   ///上传进度(总字节,上传字节,文件名)
   _txt.text = '正在上传:' + n;
   _txt.textColor = 0xff6600;
   _mc.scaleX = b/t;
  }
  public function complete(n):void
  {
   _txt.text = '上传完成:' + n;
   _txt.textColor = 0x0066cc;
   _mask.stop();
  }

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值