Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The code below compiles fine on the Flex 4 SDK on Fedora 15. Mouse-click opens the dialog box, I click okay, and a file is saved, but the file is empty. I run the same SWF file (that was compiled on the Linux machine) on a Windows machine, and the created file contains the expected data.

Then I broke the FileReference declaration out of the function into the class level, hoping to avoid a known bug and to keep a reference to the file alive outside the function. However, the same problem persists.

Hoping to set up a workaround, I added the debug Flash player to my path and ran the file from Flash without the benefit of the browser, and it works. So now a Flex problem has become a Firefox problem, maybe owing to a shady procedure I used to install the plugin without really understanding what was happening. I am running Firefox 5.0.

In essence my workflow is fixed, but perhaps people who performed the above will not be able to use projects with FileReference.save()? Should I be worried about this edge case?

/*
WriteTheFile.as
Original code by Brian Hodge
Test to see if ActionScript/Flash can write files
*/
package{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.ByteArray;
    import flash.net.FileReference;

    public class WriteTheFile extends Sprite 
    {
        private var _xml:String;
        private var fr:FileReference;

        public function WriteTheFile():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //Calling the save method requires user interaction and Flash Player 10
            stage.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
        }
        private function _onMouseDown(e:MouseEvent):void
        {
            fr = new FileReference()
            fr.save("<xml><test>data</test></xml>", "filename.txt");
        }
    }
}

EDIT: addressed answer in code above, but the same problem exists.

EDIT: This works on the same system when the standalone player is invoked. Therefore this is a browser (FF 5.0) plugin problem.

share|improve this question
3  
This should be migrated to SO as it's not really gamedev related. –  bummzack Aug 14 '11 at 9:38
    
This is for a level-building tool for Flash game. I wanted to test Flash's file saving so that I could build, test, and save levels on the same platform. –  SquareCrow Aug 15 '11 at 2:01
1  
Your question is still not gamedev related, although you use it for a level-editor. Btw: did you read this? –  bummzack Aug 15 '11 at 6:23
1  
The "shady procedure" you linked to describes installing the 64-bit square player - which is not fully supported. 64-bit is only really supported from FP11 onwards: labs.adobe.com/downloads/flashplayer11.html. Also, here's some info about 64-bit pain with FF and Ubuntu: help.ubuntu.com/community/AMD64/…, linked to from Adobe: kb2.adobe.com/cps/000/6b3af6c9.html#main_installLinux –  lukevanin Aug 15 '11 at 15:56
    
@SquareCrow If you want to read/write level-files, why not implement your level editor as an AIR application? –  bummzack Aug 15 '11 at 21:45

1 Answer 1

The problem is due to the garbage collector disposing the FileReference object before it has completed writing to the file.

As for why it works on Linux and not Windows could be attributed to a different memory management strategy in the Linux version of the player compared to the Windows version.

The solution is to keep a reference to the FileReference, eg:

public class WriteTheFile extends Sprite 
{
    private var _xml:String;
    private var fr:FileReference = new FileReference();

    private function _onMouseDown(e:MouseEvent):void
    {
        fr.save("<xml><test>data</test></xml>", "filename.txt");
    }
}
share|improve this answer
    
This answer confused me a bit before I realized that file saving is an asynchronous process, so the FileReference does indeed leave the scope of the function before it's done saving. An alternate approach might be to add an event listener to the FileReference and respond to save completion; that may prevent garbage collection, but I'm not sure. –  Gregory Avery-Weir Aug 14 '11 at 16:26
    
I moved the FileReference outside the function to the class level to keep it alive. (Edit above.) This was not a fix, but I now believe it is a browser problem. See edit. Thank you for the input. –  SquareCrow Aug 15 '11 at 2:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.