0

I would like to load a user's file right after the app is inicialized. I have this code:

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

It works fine when some button is pressed, but when i just put it inside the frame's code, it doesnt work. How can i do it?

Alena
  • 179
  • 1
  • 10

2 Answers2

1

This function works only on user interaction (mouse click, key press etc) for security reasons, so it won't work when you paste it in regular frame script.

Bartek
  • 1,986
  • 1
  • 14
  • 21
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();
    }
Swati Singh
  • 1,863
  • 3
  • 19
  • 40