虽然flash没有AIR那么大的权限,去操作和选择本地文件。但做普通的选择上传还是可以的。如果要上传到服务器上,需要借助第三方语法,比如PHP,ASPX等等。如果想更完美,更大权限的操作系统文件,请用AIR吧。可怜的AIR,境遇到了这个样子。也许,这就是移动端的限制吧。会有完美的性能和体验么。
个人,比较喜欢PHP.就用PHP吧。
先看下效果吧:
flash 选择本地文件的API是FileReference,FileReference除了有上传功能,还有下载功能。既可以上传到服务器,也可以上传到内存中,就是flash自身的加载。请看官方API介绍:
FileReference 类提供了在用户计算机和服务器之间上载和下载文件的方法。操作系统对话框会提示用户选择要上载的文件或用于下载的位置。每个 FileReference 对象都引用用户磁盘上的一个文件并且具有一些属性,这些属性包含有关文件大小、类型、名称、创建日期、修改日期和创建者类型(仅限 Macintosh)的信息。
选择磁盘里边的一个文件,请用browse方法。browse里边的参数是一个包含FileFilter的数组,FileFilter是用来描述你要选择文件的筛选条件。如果是*,就是可以选择所有的类型的文件。
假如:var fileFilter:FileFitler = new FileFilter(“Image(*.jpg;*.png)”,”*.jpg;*.png”); 这个表示,你可以浏览选择磁盘里边的jpg格式和png格式的文件。当然,你将其他格式的文件后缀名字改成jpg或png的也是可见的。
说说比较重要的事件吧。
Event.SELECT, 当你选择好一个文件点击打开的时候触发。这个时候FileReference就拥有一定的数据信息了。然会你使用FileReference的upLoad方法,可以将你所选择的文件上传到服务器上去。当然,你服务器端的程序也要配合写好才行。
Event.COMPLETE,当上传完毕后,会触发这个事件。下载也是一样的。Event.COMPLETE太广泛了,哪里都有得用。事件么,就是一种标示。只是这些系统的事件,不需要你手动去触发就能发送的哈。当然,你也可以自定义事件。
ProgressEvent.PROGRESS,对上传或下载的过程的进行侦听,这个和普通的load一样的。
个人比较喜欢php,感觉后缀php好听,做出的网页也好看(好看的功劳角色在)。所以,和web服务器交互的都用php。php的move_uploaded_file方法,是将资源里边的数据放到一个路径下。而这里的资源,就是flash所updata的了。如果是php自身,
那么是html的file所提供的。这里需要明确几点
A:Flash传递的文件表单name 属性为 Filedata。这个很重要,如果没有这个,就像进入了很多个在装有临时文件域的盒子,不知道哪个盒子才是自己上传的。
B:文件域提交到PHP那边,是一个全局的$_FILES。这个就是那些盒子的集合。有了Filedata这个key,就可以进去了。进去你会发现有tmp_name,name,type,size等属性。
C:flash上传的服务器路径最好是绝对路径,因为如果使用相对路径,组件会以swf所在位置作为起点。而不是以当前html页面所在位置为起点。
D:move_uploaded_file,是将临时文件移动到所指定的磁盘位置。它有返回值,如果是1表示上传成功啦。
E:对于html网页form的提交,在form标签中要加入enctype=”multipart/form-data”这个标记才行。
php_code:
<?php ob_start("sava_content"); echo 'test'."\r\n"; print_r($_FILES); print_r($_GET); print_r($_POST); if(move_uploaded_file ( $_FILES['Filedata']['tmp_name'], $_FILES['Filedata']['name'])) { echo '上传文件成功!'; } else { echo '上传文件失败'; } ob_end_flush(); function sava_content($buffer) { $fp = fopen('./result.txt','w'); fputs($fp,$buffer); fclose($fp); } ?>
as_code:
package com.vini123 { /** * @author vini123 整理 * @data 2014.03.25 * @blog http://blog1.vini123.com * @weixin vini1024 */ import flash.display.Bitmap; import flash.display.Loader; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.MouseEvent; import flash.events.ProgressEvent; import flash.net.FileFilter; import flash.net.FileReference; import flash.net.URLRequest; [SWF(width ="720" , height = "450" , frameRate="30")] public class Main extends Sprite { private var _file:FileReference; private var _fileFilterList:Array = []; private var _labelList:Vector.<String> = new Vector.<String>(); private const HOST:String = "http://localhost:8080/php/upload/"; //"http://localhost:8080/php/file/"; private const FILE_NAME:String = "upload.php"; private var _button:Button; private var _result:Result; private var _loading:Loading; private var _image:Bitmap; private var _imageDefaultWidth:int = 420 private var _imageDefaultHeight:int = 250; public function Main() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; _file = new FileReference(); _fileFilterList.push(new FileFilter("Image(*.jpg;*.png;*.gif)","*.jpg;*.png;*.gif"), new FileFilter("Document(*.txt;*.doc;*.docx;*.pdf)","*.txt;*.doc;*.docx;*.pdf"), new FileFilter("All Type","*")); _labelList.push("请选择要上传的文件","上传到服务器","上传到舞台","重新选择要上传的文件"); _button = new Button(_labelList[0]); _button.addEventListener(MouseEvent.CLICK , clickHandler); addChild(_button); resizeButton(); _result = new Result(); addChild(_result); _loading = new Loading(); addChild(_loading); _loading.x = stage.stageWidth * 0.5 - _loading.width * 0.5; _loading.y = stage.stageHeight - _loading.height -5; _result.x = stage.stageWidth - _result.width -10; _result.y = 10; _result.Show("可以加我微信吗:vini1024 \n请选择文件。如果可以请选择图片。"); } /** * 选择上传的文件 */ private function open():void { _result.Show("开始选择文件"); _file.browse(_fileFilterList); _file.addEventListener(Event.SELECT , selectHandler); _loading.upLoaderPercent = 0; _loading.loaderImagePercent = 0; if(_image && _image.bitmapData) { removeChild(_image); _image.bitmapData.dispose(); } } private function selectHandler(e:Event):void { _file.removeEventListener(Event.SELECT , selectHandler); if(_file.type == ".jpg" || _file.type == ".png" ||_file.type == ".gif") { _button.label = _labelList[ 1+ int((Math.random() * _labelList.length))%2]; resizeButton(); } else { _result.Show("你选择的文件不是图片类型。所以你白选了。如果你将后缀改成图片类型的也可以。只是你这样欺骗我不好吧。"); } } /** * 上传到服务器 */ private function upload():void { _result.Show("开始上传到服务器"); var urlRequest:URLRequest = new URLRequest(); urlRequest.url = HOST + FILE_NAME; _file.upload(urlRequest); _file.addEventListener(Event.COMPLETE , uploadCompleteHandler); _file.addEventListener(ProgressEvent.PROGRESS , progressHandler); } private function uploadCompleteHandler(e:Event):void { _button.label = _labelList[3]; resizeButton(); _file.removeEventListener(Event.COMPLETE , uploadCompleteHandler); _file.removeEventListener(ProgressEvent.PROGRESS , progressHandler); _result.Show("上传到服务器成功"); var loader:Loader = new Loader(); loader.load(new URLRequest(HOST + _file.name)); _result.Show(HOST + _file.name); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , ioErrorHandler); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , loadImageProgressHandler); loader.contentLoaderInfo.addEventListener(Event.COMPLETE , completeHandler); } private function ioErrorHandler(e:IOErrorEvent):void { e.target.removeEventListener(IOErrorEvent.IO_ERROR , ioErrorHandler); e.target.removeEventListener(ProgressEvent.PROGRESS , loadImageProgressHandler); e.target.removeEventListener(Event.COMPLETE , completeHandler); _result.Show("加载不成功,可能上传的文件名乱码了"); } private function loadImageProgressHandler(e:ProgressEvent):void { _loading.loaderImagePercent = e.bytesLoaded/e.bytesTotal; } private function progressHandler(e:ProgressEvent):void { _loading.upLoaderPercent = Math.round(1000 * e.bytesLoaded / e.bytesTotal)/1000; } /** * 上传到内存 */ private function load():void { _result.Show("开始上传到内存"); _file.load(); _file.addEventListener(Event.COMPLETE , loadCompleteHandler); _file.addEventListener(ProgressEvent.PROGRESS , progressHandler); } private function loadCompleteHandler(e:Event):void { _result.Show("上传到内存成功"); _button.label = _labelList[3]; resizeButton(); _file.removeEventListener(Event.COMPLETE , loadCompleteHandler); _file.removeEventListener(ProgressEvent.PROGRESS , progressHandler); var loader:Loader = new Loader(); loader.loadBytes(e.target.data); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , ioErrorHandler); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , loadImageProgressHandler); loader.contentLoaderInfo.addEventListener(Event.COMPLETE , completeHandler); resizeLoader(); } private function completeHandler(e:Event):void { e.target.removeEventListener(IOErrorEvent.IO_ERROR , ioErrorHandler); e.target.removeEventListener(ProgressEvent.PROGRESS , loadImageProgressHandler); e.target.removeEventListener(Event.COMPLETE , completeHandler); _image = e.target.content as Bitmap; if(_imageDefaultWidth / _imageDefaultHeight > _image.width / _image.height) { _image.height = _imageDefaultHeight; _image.scaleX = _image.scaleY; } else { _image.width = _imageDefaultWidth; _image.scaleY = _image.scaleX; } addChild(_image) _image.x = 450 * 0.5 - _image.width * 0.5 + 5; _image.y = 390 * 0.5 - _image.height * 0.5; } private function resizeLoader():void { } /** * 点击按钮事件 */ private function clickHandler(e:MouseEvent):void { switch(_button.label) { case _labelList[0]: case _labelList[3]: open(); break; case _labelList[1]: upload(); break; case _labelList[2]: load(); break; default: break; } } private function resizeButton():void { _button.x = stage.stageWidth - _button.width - 20; _button.y = stage.stageHeight - _button.height - 20; } } } import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFieldType; import flash.text.TextFormat; class Button extends Sprite { private var _label:String; private var _txt:TextField; private var _txtFormat:TextFormat; private var _size:int = 13; private var _txtFont:String = "Microsoft YaHei"; private var _defaultTxtColor:int = 0xffffff; private var _overTxtColor:int = 0xff0000; private var _deep:Sprite; private var _deepColor:int = 0x0099ff; private var _gapX:int = 5; private var _gapY:int = 3; public function Button(label:String):void { _label = label; _txtFormat = new TextFormat(); _txtFormat.size = _size; _txtFormat.font = _txtFont; _txt = new TextField(); addChild(_txt); _txt.defaultTextFormat = _txtFormat; _txt.autoSize = TextFieldAutoSize.LEFT; _txt.textColor = _defaultTxtColor; _txt.mouseEnabled = false; _txt.multiline = false; _txt.text = _label; _txt.x = _gapX * 0.6; _txt.y = _gapY * 0.6; _deep = new Sprite(); addChildAt(_deep , 0); resizeDeep(); this.buttonMode = true; this.addEventListener(MouseEvent.ROLL_OVER , overHandler); this.addEventListener(MouseEvent.ROLL_OUT , outHandler); } private function resizeDeep():void { _deep.graphics.clear(); _deep.graphics.beginFill(_deepColor); _deep.graphics.drawRoundRect(0,0,_txt.textWidth + _gapX *2 ,_txt.textHeight + _gapY *2 , _gapX , _gapX); _deep.graphics.endFill(); } public function get label():String { return _label; } public function set label(value:String):void { _label = value; _txt.text = _label; resizeDeep(); } private function outHandler(e:MouseEvent):void { _txt.textColor = _defaultTxtColor; } private function overHandler(e:MouseEvent):void { _txt.textColor = _overTxtColor; } } class Result extends Sprite { private var _size:int = 14; private var _txtFont:String = "Microsoft YaHei"; private var _txt:TextField; private var _txtFormat:TextFormat; public function Result():void { init(); } private function init():void { _txtFormat = new TextFormat(); _txtFormat.bold = true; _txtFormat.font = _txtFont; _txtFormat.size = _size; _txtFormat.color = 0xff0000; _txt = new TextField(); _txt.border = true; _txt.borderColor = 0x0099ff; _txt.multiline = true; _txt.wordWrap = true; _txt.autoSize = TextFieldAutoSize.LEFT; _txt.type = TextFieldType.INPUT; _txt.width = 250 ; _txt.height = 400 ; _txt.defaultTextFormat = _txtFormat; addChild(_txt); } public function Show(str:String):void { trace(_txt.numLines); _txt.appendText(str + "\n"); if(_txt.numLines > 20) { _txt.text = ""; _txt.appendText(str + "\n"); } } } class Loading extends Sprite { private var _deepLine:Sprite; private var _loadLine:Sprite; private var _imageLoadLine:Sprite; private var _deepLineColor:int = 0x979193; private var _loadLineColor:int = 0x88AA00; private var _imageLoadLineColor:int = 0xffff00; private var _gap:int = 10; public function Loading():void { addEventListener(Event.ADDED_TO_STAGE , addStageHandler); } private function addStageHandler(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE , addStageHandler); _deepLine = new Sprite(); addChild(_deepLine) _loadLine = new Sprite(); addChild(_loadLine); _imageLoadLine = new Sprite(); addChild(_imageLoadLine); _deepLine.graphics.clear(); _deepLine.graphics.beginFill(_deepLineColor); _deepLine.graphics.drawRect(0,0,stage.stageWidth - _gap *2 ,_gap); _deepLine.graphics.endFill(); _loadLine.graphics.clear(); _loadLine.graphics.beginFill(_loadLineColor); _loadLine.graphics.drawRect(0,0,stage.stageWidth - _gap *2 ,_gap); _loadLine.graphics.endFill(); _loadLine.scaleX = 0; _imageLoadLine.graphics.clear(); _imageLoadLine.graphics.beginFill(_imageLoadLineColor); _imageLoadLine.graphics.drawRect(0,0,stage.stageWidth - _gap *2 ,_gap); _imageLoadLine.graphics.endFill(); _imageLoadLine.scaleX = 0; } public function set loaderImagePercent(value:Number):void { trace("g:" + value); _imageLoadLine.scaleX = value; } public function set upLoaderPercent(value:Number):void { _loadLine.scaleX = value; } }