Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:04:38+00:00 2026-06-04T10:04:38+00:00

I read file from user using FileReference. I have a variable which I want

  • 0

I read file from user using FileReference. I have a variable which I want to initialise with content of the selected file. I create a Model class with setters and getters. Although this doesn’t really work as it throws me an error telling that end of the file is reached. How can I update variable with the file? Thank you.

public class Main extends Sprite 
{
    public var fr:FileReference;
    private var _model:Model;
    private var button:Sprite;

    public function Main():void 
    {   
        _model = new Model();
        _model.addEventListener(Model.VALUE_CHANGED, fileLoaded);
        trace(_model.getVariable());
    }
    protected function loadClicked(e:Event):void
    {
        fr = new FileReference();
        fr.addEventListener(Event.SELECT, fileSelected);
        fr.browse([new FileFilter("JSON Files (*.json)", "*.json")]);
    }
    public function fileSelected(event:Event):void 
    {
        fr.addEventListener(Event.COMPLETE, fileLoaded);
        fr.load();
    }
    public function fileLoaded(event:Event):void
    {
        try
        {
            var content:ByteArray = fr.data;
            _model.setVariable(content.readUTFBytes(content.length));
        }
        catch (e:Error)
        {
            trace("Error");
        }
    }
}

public class Model extends EventDispatcher
{
    public static const VALUE_CHANGED:String = "value_changed";
    private var variable:String;
    public function Model() :void
    {

    }
    public function setVariable(newVar:String):void
    {
        variable = newVar;
        this.dispatchEvent(new Event(Model.VALUE_CHANGED));
    }
    public function getVariable():String {
        return variable;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:04:40+00:00Added an answer on June 4, 2026 at 10:04 am

    You have a mistake there:

    _model.addEventListener(Model.VALUE_CHANGED, fileLoaded); 
    // ...
    fr.addEventListener(Event.COMPLETE, fileLoaded);
    

    Notice, that both events trigger the same handler.
    This is obviously not what you wanted.

    And btw, while calling readUTFBytes() on freshly loaded ByteArray is Ok (as position is initially zero), it is generally good to set position before reading. Just to keep things clear.

    var content:ByteArray = fr.data;
    content.position = 0;
    _model.setVariable(content.readUTFBytes(content.length));
    

    Corrected version of your code:

    package 
    {
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.net.FileFilter;
        import flash.net.FileReference;
        import flash.utils.ByteArray;
    
        [SWF(width="800", height="600")]
        public class Main extends Sprite 
        {
            public var fr:FileReference;
            private var _model:Model;
            private var button:Sprite;
    
            public function Main():void 
            {   
                _model = new Model();
                _model.addEventListener(Model.VALUE_CHANGED, valueChanged);
                stage.addEventListener(MouseEvent.MOUSE_DOWN, loadClicked);
                trace(_model.getVariable());
            }
    
            protected function valueChanged(event:Event):void
            {
                trace(_model.getVariable());
            }
            protected function loadClicked(e:Event):void
            {
                fr = new FileReference();
                fr.addEventListener(Event.SELECT, fileSelected);
                fr.browse([new FileFilter("JSON Files (*.json)", "*.json")]);
            }
            public function fileSelected(event:Event):void 
            {
                trace(event.type);
                fr.addEventListener(Event.COMPLETE, fileLoaded);
                fr.load();
            }
            public function fileLoaded(event:Event):void
            {
                trace(event.type);
                try
                {
                    var content:ByteArray = fr.data;
                    content.position = 0;
                    _model.setVariable(content.readUTFBytes(content.length));
                } catch (e:Error) {
                    trace("Error");
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read the file path from html input type=file (the entry selected
I want to read a file from a java web application. I don't want
I need to read a file from the local file system using JSP, save
I'm trying to read a file from a local filesystem. I do not have
Is there way that I can read the file from remote server using fopen
i have csv files, java app and database, i read csv file from my
How to Write/Read a file to/from a network folder/share using python? The application will
I have a web application which I uploaded using IIS. I want the users
I have a program that load data from a file using std::ifstream and store
having read the docs, https://docs.djangoproject.com/en/dev/topics/signals/ i have created this in my signals.py file: from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.