3

How can save data into a XML file without user prompt dialog in Action Script 3?

I'm writing an simple application (with adobe flash) that runs on client PC and save user state data in XML file in same directory of app. When i use FileReference it shows a user dialog for saving file. Is there any class to save just XML data directly into XML file?

I think writing just XML (text plane) data couldn't make any security problems? :-?

2
  • Are you intending this to be an AIR application, a Flash projector, or something that runs in the browser? Commented Mar 12, 2011 at 21:51
  • The application runs independent as an SWF or Flash Projector on client PC without using browser. Commented Mar 13, 2011 at 10:19

3 Answers 3

3

Im amazed noone has posted this already. Load the xml file into memory using URLLoader, making sure the dataFormat is URLLoaderDataFormat.TEXT, then write it to a file using a filestream.

Working code is posted below

Hope it helps

private function loadConfigFromServer():void{
    var request:URLRequest = new URLRequest(serverConfigXmlLocation);
    configLoader = new URLLoader();

    configLoader.addEventListener(Event.COMPLETE, configLoadCompleteHandler);
            configLoader.addEventListener(IOErrorEvent.IO_ERROR, configLoadIOErrorEventHandler);
    configLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, configLoadSecurityErrorEventHandler);

    configLoader.dataFormat = URLLoaderDataFormat.TEXT;
    configLoader.load(request);

}


 private function configLoadCompleteHandler(event:Event):void{
     configLoader.removeEventListener(Event.COMPLETE, configLoadCompleteHandler);
     configLoader.removeEventListener(IOErrorEvent.IO_ERROR, configLoadIOErrorEventHandler);
     configLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,   configLoadSecurityErrorEventHandler);
     trace("config download complete");
     var fs:FileStream = new FileStream();
     try{
         fs.open(new File(localConfigXmlLocation), FileMode.WRITE);
         fs.writeMultiByte(configLoader.data, "unicode");
     }catch(error:IOError){
         trace("IOError saving config xml");
     }catch(error:SecurityError){
         trace("SecurityError saving config xml");
     }finally{
         fs.close();
         parseLocalConfig();
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 & Thank you! but i switched to Adobe Air, 10 months ago :)
2

The only real way to save data without a dialog coming up is through the SharedObject class. Provided you don't need to edit the XML externally, it should be fine, you can save it.

SharedObject will put up a dialog however if you go above 100KB (i think) of data, so if your XML is getting larger than this, compress it using a ByteArray.

Compressing:

var ba:ByteArray = new ByteArray;
ba.writeUTFBytes( myXML );
ba.compress();

Uncompressing:

try
{
    ba.uncompress();
}
catch ( e:Error )
{
    trace( "The ByteArray wasn't compressed!" );
}

// set our xml data
myXML = XML( ba );

3 Comments

Can i use SharedObject to specify directory of my XML file?
By the way, deflate is better than compress.
Your SharedObject is stored locally on your computer (see macromedia.com/support/documentation/en/flashplayer/help/…) - you don't need to set the directory. It's pretty easy to use - it's the same as storing data in an Object. Check out the docs for more info: help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/…
1

Possible duplicate: Saving XML file in AS3 is possible

With a shared object like divillysausages described, you can save an array, or XML data, or other variables to a shared object to retrieve it later. You're only saving this data to the users computer, so it can't be used anywhere but locally on their machine again. Short answer, without a server-side language to communicate with, you cannot save data to an actual XML file if it's an-browser app or a projector.

2 Comments

Thank you but that answer in link uses FileReference! :-<
For security reasons, I can't imagine that Flash player would let anyone unknowingly save a file. The only method I can think of that wouldn't prompt the user is just saving the data to a remote server as POST data or something. That scenario though would assume that the user has an active connection to the internet.

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.