我想在应用程序初始化后加载用户的文件。我有这样的代码:
fll = new FileReference();
fll.browse([new FileFilter("Text *.txt", "*.txt")]);
当一些按钮被按下时,它工作得很好,但是当我把它放在框架的代码中时,它不起作用。我该怎么做呢?
出于安全原因,此函数仅适用于用户交互(鼠标点击,按键等),因此当您将其粘贴到常规框架脚本中时将不起作用。
Try this code, hopefully this will work for you:
import flash.text.*;
import flash.net.FileReference;
var tf:TextField = new TextField();
tf.width = 350
tf.height = 350
tf.multiline=true;
tf.border = true
addChild(tf);
var fileReference:FileReference = new FileReference();
var fileFilter:FileFilter=new FileFilter("Images","*.txt");
fileReference.addEventListener(Event.SELECT, onSelectFile);
fileReference.addEventListener(Event.COMPLETE,onFileComplete);
bBrowse.addEventListener(MouseEvent.MOUSE_DOWN, browseFile);
function onFileComplete(event:Event):void
{
tf.htmlText = "opening: "+fileReference.name+"<br>"
var bytes:ByteArray=fileReference.data;
var newtext:String=fileReference.data.readMultiByte(fileReference.data.length,"gb2312");
//var newtext:String = fileReference.data.readUTFBytes(fileReference.data.length)
tf.appendText(newtext);
}
function browseFile(e:MouseEvent)
{
fileReference.browse([fileFilter]);
}
function onSelectFile(event:Event):void
{
fileReference.load();
}