最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
js与flash配合读取本地文件内容到页面中例子
时间:2016-03-18 编辑:简简单单 来源:一聚教程网
本文也没什么技术可说的,在说主题前,我还想吐槽一下,自己没写过flash,都是通过百度然后再修修改改才得以实现的,本来是想找已离职同事(flash程序员)帮忙写的,结果不是说忙就是说没做过这样的功能,不知道是不想帮还是……
算了,不说了,直接上代码:
AS3代码:
import flash.events.*;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.external.ExternalInterface;
var fileRef:FileReference;
this.stage.addEventListener(MouseEvent.CLICK,clickAction);
function clickAction(evt:Event):void
{
fileRef = new FileReference();
fileRef.addEventListener(Event.SELECT, onFileSelected);
fileRef.addEventListener(Event.CANCEL, onCancel);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
var textTypeFilter:FileFilter = new FileFilter("请选择文本文件 (*.txt)","*.txt");
fileRef.browse([textTypeFilter]);
}
function onFileSelected(evt:Event):void
{
fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress);
fileRef.addEventListener(Event.COMPLETE, onComplete);
fileRef.load();
}
function onProgress(evt:ProgressEvent):void
{
//trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes.");
ExternalInterface.call('getFileContent','loading','正在读取文件中...'+(evt.bytesLoaded/evt.bytesTotal*100)+'%'); /// as调用js的方法
}
function onComplete(evt:Event):void
{
//trace("File was successfully loaded.");
//trace(fileRef.data);
//ExternalInterface.addCallback(); /// 提供给js调用的as方法
ExternalInterface.call('getFileContent','complete',fileRef.data.toString()); /// as调用js的方法
}
function onIOError(evt:IOErrorEvent):void
{
//trace("There was an IO Error.");
ExternalInterface.call('getFileContent','error',"读取文件失败"); /// as调用js的方法
}
function onSecurityError(evt:Event):void
{
//trace("There was a security error.");
ExternalInterface.call('getFileContent','error',"由于浏览器安全限制无法读取文件"); /// as调用js的方法
}
function onCancel(evt:Event):void
{
//trace("The browse request was canceled by the user.");
}
js代码:
// 供flash调用
function getFileContent(){
console.log(arguments)
}
代码就这么多了。本来是想通过js+服务端实现,但需求没说让移动端兼容,所以用flash可以减少服务端的请求而且读取文件内容的速度也快。
虽然flash是一个过时的东西,还是记录在这里吧,以便以后会用。
-
上一个: Iframe根据内容自适应高度和宽度
-
下一个: js中利用JSONP解决跨域问题
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31