Save to XML file without user prompt dialog in action script 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? :-?
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();
}
}
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 );
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.
精彩评论