0

我想在应用程序初始化后立即加载用户的文件。我有这个代码:

fll = new FileReference();          
fll.browse([new FileFilter("Text *.txt", "*.txt")]);

当按下某个按钮时它工作正常,但是当我把它放在框架的代码中时,它不起作用。我该怎么做?

4

2 回答 2

1

出于安全原因,此功能仅适用于用户交互(鼠标单击、按键等),因此当您将其粘贴到常规框架脚本中时,它将不起作用。

于 2011-09-30T09:58:49.297 回答
0
   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();
    }
于 2011-09-30T11:01:38.813 回答