Skip to main content
August 9, 2011
Answered

Not possible to store multiple fileReference object in same array?

  • August 9, 2011
  • 1 reply
  • 620 views

Hi all,

I'm trying to store multiple fileReference  object in one array. But everytime I push() in a new object the old  objects that's already in the array gets set to the latest object.

I'm  letting the user select an image. Then I push the selected  fileReference object into an array like my_array.push(  FileReference(event.target) );

If I then run a loop on  "my_array" to check it's content I'm getting the latest pushed  fileReference object for every index in the array.

With one object pushed in:

1. Image1.jpg

With two objects pushed in:

1. Image2.jpg

2. Image2.jpg

Anyone have any thoughts on this?

Thank you so much

This topic has been closed for replies.
Correct answer chris.campbell

Hello,

Sorry for the delay.  You've probably discovered the answer for this by now, but in case you haven't or someone else runs into this issue here's code that should fix this up:

// Local Files private var fileList:Array = new Array(); private var localFiles:FileReference = null; private function browse(event:MouseEvent):void {      localFiles = new FileReference();      trace("[Browse] Using local files");      localFiles.addEventListener(Event.SELECT, onLocalFilesSelected);      localFiles.browse(); }

The problem was you had a single instance of localFiles, and while you were storing it multiple times in the array, when the reference was changed it effected all of the array.  Instead, I'm newing up a FileReference in the browse() function, so that we get a new one each time.  Hopefully that makes sense.

Chris

1 reply

chris.campbell
Legend
August 9, 2011

Would you mind posting your code so we can review it?

Thanks,

Chris

August 10, 2011

This is basically what I'm doing. The code is cut out from a larger portion so there might be errors, but hopefully not.

// Local Files
private var localFiles:FileReference           = new FileReference();
private var fileList:Array                = new Array();

private function browse(event:MouseEvent)
{
     trace("[Browse] Using local files");
     localFiles.addEventListener(Event.SELECT, onLocalFilesSelected);
     localFiles.browse(getAllowedTypes());
}

/*
* LOCAL FILE
* -----------------------------------------------------------------------------------------
*/
          
private function onLocalFilesSelected(e:Event)
{
     localFiles.addEventListener(Event.COMPLETE, localFileHandler);
     localFiles.load();
}
          
private function localFileHandler(event:Event):void
{
     localFiles.removeEventListener(Event.COMPLETE, localFileHandler);

     fileList.push(FileReference(event.target));

     showFileList();
}

private function showFileList():void
{
     for(var $x:int=0;$x<fileList.length;$x++)
     {                    
          var file:FileReference = FileReference(fileList[$x]);
                    
          trace($x+": " + file.name);
     }
}

Thank you

chris.campbell
chris.campbellCorrect answer
Legend
August 15, 2011

Hello,

Sorry for the delay.  You've probably discovered the answer for this by now, but in case you haven't or someone else runs into this issue here's code that should fix this up:

// Local Files private var fileList:Array = new Array(); private var localFiles:FileReference = null; private function browse(event:MouseEvent):void {      localFiles = new FileReference();      trace("[Browse] Using local files");      localFiles.addEventListener(Event.SELECT, onLocalFilesSelected);      localFiles.browse(); }

The problem was you had a single instance of localFiles, and while you were storing it multiple times in the array, when the reference was changed it effected all of the array.  Instead, I'm newing up a FileReference in the browse() function, so that we get a new one each time.  Hopefully that makes sense.

Chris