如何在Flash中加载用户的文件?

时间:2011-09-30 09:51:53

标签: flash actionscript-3 filereference

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

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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();
    }