FileReference选择加载本地文件

FileReference 支持Flash从用户计算机中(本地)获得文件一些信息和上载、下载操作,每个FileReference对象引用用户磁盘上的一个文件。

FileReference 对象通过browse()方法打开选择文件对话框并选择一个文件,选择成功FileRefercence对象会派发Event.SELECT事件,在select事件中调用load()方法开始加载用户选择的本地文件,当完成加载时文件时其内容作为字节数组存储在FileRefercence.data属性中。

package util {
	import events.FileOpenDialogEvent;
	import flash.events.ProgressEvent;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	/**
	 * 文件浏览对话框
	 * @author Wing
	 */
	[Event(name = "file_size_error", type = "events.FileOpenDialogEvent")];
	[Event(name = "file_max_size_limit", type = "events.FileOpenDialogEvent")];
	[Event(name = "select_file", type = "events.FileOpenDialogEvent")];
	[Event(name = "cancel", type = "events.FileOpenDialogEvent")];
	public class FileOpenDialog extends EventDispatcher 
	{
		private var _maxSize:int = 2097152; //文件最大尺寸
		private var fileRef:FileReference;
		private var loader:Loader;
		public function FileOpenDialog() 
		{
			fileRef = new FileReference();
		
		}
		/**
		 * 打开文件浏览对话框
		 * @param	filterType
		 */
		public function browse(filterArr:Array):void
		{

			addBrowseEventListener();
			fileRef.browse(filterArr);
		}
		
		/**
		 * 文件的大小
		 * @return 如果出现异常,无法读取正确文件的大小则返回-1
		 */
		public function get fileSize():Number
		{
			try 
			{
				return fileRef.size;
			}catch (err:Error)
			{
				trace(err.message);
				
				this.dispatchEvent(new FileOpenDialogEvent(FileOpenDialogEvent.FILE_SIZE_ERROR));
				return -1;
			}
			return -1;
		}
		
		public function get maxSize():int 
		{
			return _maxSize;
		}
		
		public function set maxSize(value:int):void 
		{
			_maxSize = value;
		}
		
		/**
		 * 添加打开文件浏览对话框事件监听
		 */
		private function addBrowseEventListener():void
		{
			fileRef.addEventListener(Event.SELECT, fileSelectHandler);
			fileRef.addEventListener(Event.CANCEL, fileCancelHandler);
		}
		
		/**
		 * 删除打开文件浏览对话框事件监听
		 */
		private function removeBrowseEventListener():void
		{
			fileRef.removeEventListener(Event.SELECT, fileSelectHandler);
			fileRef.removeEventListener(Event.CANCEL, fileCancelHandler);
		}
		
		/**
		 * 选择文件事件处理
		 */
		private function fileSelectHandler(e:Event):void
		{
			<span style="font-family:宋体;">trace(</span>"select file size:" + fileSize);
			removeBrowseEventListener();
			if (fileSize == -1)
			{
				return;
			}
			if (fileSize > maxSize)
			{
				var dialogEvent:FileOpenDialogEvent = new FileOpenDialogEvent(FileOpenDialogEvent.FILE_MAX_SIZE_LIMIT);
				var data:Object = { size:fileSize };
				dialogEvent.data = data;
				this.dispatchEvent(dialogEvent);
				return;
			}
			addLoadListeners();
			fileRef.load();
		}
		
		/**
		 * 文件浏览对话框取消选择文件事件处理
		 */
		private function fileCancelHandler(e:Event):void
		{
			removeBrowseEventListener();
			this.dispatchEvent(new FileOpenDialogEvent(FileOpenDialogEvent.CANCEL));
		}
		
		private function addLoadListeners():void
		{
			fileRef.addEventListener(Event.COMPLETE, fileLoadCompleteHandler);
			fileRef.addEventListener(Event.OPEN, fileLoadOpenHandler);
			fileRef.addEventListener(IOErrorEvent.IO_ERROR, fileLoadIOErrorHandler);
			fileRef.addEventListener(ProgressEvent.PROGRESS, fileLoadProgerssHandler);
		}
		
		private function removeLoadListeners():void
		{
			fileRef.removeEventListener(Event.COMPLETE, fileLoadCompleteHandler);
			fileRef.removeEventListener(Event.OPEN, fileLoadOpenHandler);
			fileRef.removeEventListener(IOErrorEvent.IO_ERROR, fileLoadIOErrorHandler);
			fileRef.removeEventListener(ProgressEvent.PROGRESS, fileLoadProgerssHandler);
		}
		
		private function fileLoadCompleteHandler(e:Event):void
		{
			trace("load file complete");
			removeLoadListeners();
			var bytes:ByteArray = (e.target as FileReference).data;
			var operateEvent:FileOpenDialogEvent = new FileOpenDialogEvent(FileOpenDialogEvent.SELECT_FILE);
			operateEvent.data = bytes;
			this.dispatchEvent(operateEvent);
		}
		
		private function fileLoadOpenHandler(e:Event):void
		{
			this.dispatchEvent(e);
		}
		
		private function fileLoadIOErrorHandler(e:IOErrorEvent):void
		{
			removeLoadListeners();
			this.dispatchEvent(e);
		}
		
		private function fileLoadProgerssHandler(e:ProgressEvent):void
		{
			this.dispatchEvent(e);
		}
	}

}

ImageFile类通过FileOpenDialog类来获取图片

package util {
	import events.FileOpenDialogEvent;
	import flash.display.LoaderInfo;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	/**
	 * 图像选择加载类
	 * @author Wing
	 */
	[Event(name="file_size_error", type="events.FileOpenDialogEvent")]
	[Event(name="file_max_size_limit", type="events.FileOpenDialogEvent")]
	[Event(name="select_file", type="events.FileOpenDialogEvent")]
	[Event(name="cancel", type="events.FileOpenDialogEvent")]
	[Event(name="ioError", type="flash.events.IOErrorEvent")]
	public class ImageFile extends EventDispatcher
	{
		private var openDialog:FileOpenDialog;
		private var fileFilterArr:Array;
		private var _bitmap:Bitmap;
		private var loader:Loader;
		private var _maxWidth:Number;
		private var _maxHeight:Number;
		public function ImageFile() 
		{
			openDialog = new FileOpenDialog();
			fileFilterArr = [new FileFilter("图片文件", "*.jpg;*.gif;*.png;*.jpeg")];
		}
		
		/**
		 * 选择图片
		 */
		public function browse():void
		{
			removeDialogListeners();
			clearLoader();
			addDialogListeners();
			openDialog.browse(fileFilterArr);
		}
		/**
		 * 设置文件最大尺寸
		 */
		public function set maxSize(value:Number):void
		{
			openDialog.maxSize = value;
		}
		/**
		 * 获得文件最大尺寸
		 */
		public function get maxSize():Number
		{
			return openDialog.maxSize;
		}
		/**
		 * 获得文件的尺寸
		 */
		public function get fileSize():Number
		{
			return openDialog.fileSize;
		}
		/**
		 * 获得位图对象
		 */
		public function get bitmap():Bitmap 
		{
			return _bitmap;
		}
		/**
		 * 清空
		 */
		public function dispose():void
        {
			removeDialogListeners();
			clearBitmap();
			clearLoader();
        }
		/**
		 * 清除位图实例
		 */
		private function clearBitmap():void
		{
			if(_bitmap)
            {
				if (_bitmap.parent)
					_bitmap.parent.removeChild(_bitmap);
				if(_bitmap.bitmapData)
					_bitmap.bitmapData.dispose();
				_bitmap = null;
            }
		}
		
		private function clearLoader():void
		{
			if (loader)
			{
				loader.unloadAndStop();
				removeLoadListeners();
			}
		}
		
		private function onMaxSizeLimitHandler(e:FileOpenDialogEvent):void
		{
			this.dispatchEvent(e);
		}
		
		private function onSelectFileHandler(e:FileOpenDialogEvent):void
		{
			//选择位图后获得该位图的字节数据
			var data:ByteArray = e.data as ByteArray;
			removeDialogListeners();
			if (loader == null)
			{
				loader = new Loader();
			}
			
			addLoadListeners();
			//var context:LoaderContext = new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain));
			loader.loadBytes(data);//加载位图字节数据
		}
		
		private function onCancelHandler(e:FileOpenDialogEvent):void
		{
			removeDialogListeners();
			this.dispatchEvent(e);
		}
		
		private function onLoadOpenHandler(e:Event):void
		{
			this.dispatchEvent(e);
		}
		
		private function onLoadIOErrorHandler(e:IOErrorEvent):void
		{
			removeDialogListeners();
			this.dispatchEvent(e);
		}
		
		private function addDialogListeners():void 
		{
			openDialog.addEventListener(FileOpenDialogEvent.FILE_MAX_SIZE_LIMIT, onMaxSizeLimitHandler);
			openDialog.addEventListener(FileOpenDialogEvent.SELECT_FILE, onSelectFileHandler);
			openDialog.addEventListener(FileOpenDialogEvent.CANCEL, onCancelHandler);
			openDialog.addEventListener(Event.OPEN, onLoadOpenHandler);
			openDialog.addEventListener(IOErrorEvent.IO_ERROR, onLoadIOErrorHandler);
		}
		
		private function removeDialogListeners():void
		{
			openDialog.removeEventListener(FileOpenDialogEvent.FILE_MAX_SIZE_LIMIT, onMaxSizeLimitHandler);
			openDialog.removeEventListener(FileOpenDialogEvent.SELECT_FILE, onSelectFileHandler);
			openDialog.removeEventListener(FileOpenDialogEvent.CANCEL, onCancelHandler);
			openDialog.addEventListener(Event.OPEN, onLoadOpenHandler);
			openDialog.addEventListener(IOErrorEvent.IO_ERROR, onLoadIOErrorHandler);
		}
				
		private function addLoadListeners():void
		{
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderCompleteHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoaderErrorHandler);
		}
		
		private function removeLoadListeners():void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaderCompleteHandler);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoaderErrorHandler);
		}
		
		private function onLoaderCompleteHandler(e:Event):void
		{
			trace("Loade file completed:" );
			removeLoadListeners();
			trace("Loade file completed:"+e.target.content );
			var bmp:Bitmap =  (e.target as LoaderInfo).content as Bitmap;
			trace("Loade bmp:"+bmp );
			var dialogEvent:FileOpenDialogEvent = new FileOpenDialogEvent(FileOpenDialogEvent.SELECT_FILE);
			var data:Object = {"success":"false","width":bmp.width,"height":bmp.height,"size":fileSize};
			if((isNaN(_maxWidth) || bmp.width <= _maxWidth) &&
				(isNaN(_maxHeight) || bmp.height <= _maxHeight))
			{
				clearBitmap();
				_bitmap = bmp;
				data["success"] = "true";
				data["bitmap"] = bmp;
			/*	var event:FileOpenDialogEvent = new FileOpenDialogEvent(FileOpenDialogEvent.SELECT_FILE);
				event.data = _bitmap;
				this.dispatchEvent(event);*/
			}
			
			trace("Loade file completed success:"+data.success);
/*			if((!isNaN(_maxWidth) && bmp.width >_maxWidth) ||
				(!isNaN(_maxHeight) && bmp.height >_maxHeight))
			{
				return;
			}*/
			dialogEvent.data = data;
			this.dispatchEvent(dialogEvent);
		
		}
		
		private function onLoaderErrorHandler(e:IOErrorEvent):void
		{
			removeLoadListeners();
			this.dispatchEvent(e);
		}

		public function get maxWidth():Number
		{
			return _maxWidth;
		}

		public function set maxWidth(value:Number):void
		{
			_maxWidth = value;
		}

		public function get maxHeight():Number
		{
			return _maxHeight;
		}

		public function set maxHeight(value:Number):void
		{
			_maxHeight = value;
		}


	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值