0

I am trying to create a psychological experiment in ActionScript. The performance of the participants is to be stored in a separate .csv file. I have written this code (apparently, instead of "This is the text to write" there is going to be a data array, but the problem appears with this code on equal parts)

import flash.net.FileReference;
import flash.events.Event;

var hint:String = "Please give a name for the file";
var labelling:String;

experiment_label.addEventListener(KeyboardEvent.KEY_UP, enter_pressed)

function enter_pressed (event:KeyboardEvent):void{
    if (event.keyCode == 13) {
        labelling = experiment_label.text;

        addEventListener(Event.ENTER_FRAME,saveFile);
        var ss:String = "this is text to write";
        var fileRef:FileReference;
        fileRef = new FileReference();
        function saveFile(event:Event):void
            {
                fileRef.save(ss, labelling+".csv");

                removeEventListener(Event.ENTER_FRAME, saveFile);
            }
    }
}

The problem I am facing is as follows: when I run it in from under Flash, it operates perfectly, and the save window does pop up. However, if I run an .swf file separately, it just ignores saving command.

Could you kindly suggest, what can I do? Or maybe I should use a different approach to saving altogether?

1 Answer 1

1

You current code will give you an Error #2176 (use a try ... catch to catch it) because FileReference.save()

is not called in response to a user action, such as a mouse event or keypress event.

To avoid that, you have to remove the Event.ENTER_FRAME event listener, even you don't need it :

function on_KeyUp(event:KeyboardEvent):void
{
    if (event.keyCode == 13)
    {
        save_File();
    }
}

function save_File():void
{
    try
    {   
        var fileRef:FileReference = new FileReference();
            fileRef.save('some text here', 'my_file.csv');
    }
    catch (e:Error)
    {
        trace(e.toString());
    }
}

Hope that can help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a great lot! It actually did help! Unfortuantely, I am a newbie, and I cannot vote for your answer, but my deepest gratitude is with you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.