2 Replies Latest reply: May 31, 2010 5:28 PM by Andrei1 RSS

    Opening Help

    Fumph Community Member

      I created an Open button but it all ways comes up with an error. Here is my code:

       

      import flash.filesystem.*;
      import flash.events.Event;
      import flash.net.FileFilter;
      open_btn.addEventListener(MouseEvent.MOUSE_DOWN, Open);
      function Open(event:MouseEvent):void {
      var fileToOpen:File = new File();
      var picFilter:FileFilter=new FileFilter("Pictures", "*.png;*.jpeg;*.gif;*.bmp;");
      try {
        fileToOpen.browseForOpen("Open", [picFilter]);
        fileToOpen.addEventListener(Event.SELECT, fileSelected);
      } catch (error:Error) {
        trace("Failed:", error.message);
      }
      function fileSelected(event:Event):void {
        var stream:FileStream = new FileStream();
        stream.open(File, FileMode.READ);
        var fileData:String=stream.readUTFBytes(stream.bytesAvailable);
        trace(fileData);
      }
      }

       

      Try this and tell me if you get a problem?

        • 1. Re: Opening Help
          Andrei1 Community Member

          First, when you have an error, please post error text with your requests.

           

          Second, I hope this AIR application. Otherwise you cannot use flash.filesystem package.

           

          Third, the line

           

          stream.open(File, FileMode.READ);

           

          is wrong. It references File as a Class - not instance. Line must be:

           

          stream.open(new File(), FileMode.READ);

          • 2. Re: Opening Help
            Andrei1 Community Member

            Actually, the line is wrong altogether for the first argument in stream.open must be selected file which is event's target. So the correct line is:

             

            stream.open(File(event.target), FileMode.READ);