我正在尝试在flash(AS3)中创建一个表单,该表单可以将数据/文本从多个动态文本框保存到用户指定的路径&本地档案&还将任何已保存的表单加载回相应的文本框中。我已经找到了一些关于如何使用单个文本框执行此操作的很好的示例。我知道它应该很简单,但我似乎无法弄明白或成功修改现有的例子来处理多个文本框?有任何想法吗?
这是我目前使用/尝试修改的代码。
package
{
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.IOErrorEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.utils.ByteArray;
import flash.xml.XMLDocument;
public class BinaryXMLTool extends Sprite
{
// ** minimalist buttons **
private var browseButton:TextField;
private var saveButton:TextField;
// ** minimalist editor **
private var xmlText:TextField;
// ** browse/load/save **
private var xmlFile:FileReference;
public function BinaryXMLTool():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
stage.align = StageAlign.LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.showDefaultContextMenu = false;
// ** draw minimalist text editor **
xmlText = new TextField();
xmlText.multiline = true;
xmlText.background = true;
xmlText.backgroundColor = 0xEEEEEE;
xmlText.type = TextFieldType.INPUT;
xmlText.width = 400;
xmlText.height = 400;
xmlText.border = true;
xmlText.x = 15;
addChild(xmlText);
// ** draw minimalist browse button **
browseButton = new TextField();
browseButton.autoSize = TextFieldAutoSize.LEFT;
browseButton.background = true;
browseButton.backgroundColor = 0x000000;
browseButton.defaultTextFormat = new TextFormat("Tahoma", 14, 0xFFFFFF, true, null, null, null, null, null, 4, 4);
browseButton.selectable = false;
browseButton.text = "BROWSE";
browseButton.x = (stage.stageWidth - browseButton.width) * 0.5;
browseButton.y = 420;
addChild(browseButton);
// ** draw minimalist save button **
saveButton = new TextField();
saveButton.autoSize = TextFieldAutoSize.LEFT;
saveButton.background = true;
saveButton.backgroundColor = 0xAAAAAA;
saveButton.defaultTextFormat = browseButton.defaultTextFormat;
saveButton.selectable = false;
saveButton.text = "SAVE";
saveButton.x = (stage.stageWidth - saveButton.width) * 0.5;
saveButton.y = 460;
addChild(saveButton);
// ** button listeners **
browseButton.addEventListener(MouseEvent.CLICK, on_buttonClick, false, 0, true);
saveButton.addEventListener(MouseEvent.CLICK, on_buttonClick, false, 0, true);
}
/**
* handle browse or save
*/
private function on_buttonClick(evt:MouseEvent):void
{
var btn:TextField = evt.target as TextField;
if (btn)
{
if (btn.text == "BROWSE")
{
xmlFile = new FileReference();
xmlFile.addEventListener(Event.SELECT, on_xmlSelect, false, 0, true);
xmlFile.browse([new FileFilter("XML Documents","*.xml")]);
}
else if (btn.text == "SAVE")
{
if (xmlFile)
{
if (xmlText.text.length)
{
// ** saving as binary **
var data:ByteArray = new ByteArray();
data.writeUTFBytes(xmlText.text);
data.compress();
new FileReference().save(data, "bin" + xmlFile.name);
}
}
}
}
}
/**
* handle browse, load XML file
*/
private function on_xmlSelect(evt:Event):void
{
xmlFile.removeEventListener(Event.SELECT, on_xmlSelect);
xmlFile.addEventListener(Event.COMPLETE, on_xmlComplete, false, 0, true);
xmlFile.load();
}
/**
* handle load, check if it is binary, uncompress, display XML in editor
*/
private function on_xmlComplete(evt:Event):void
{
xmlFile.removeEventListener(Event.COMPLETE, on_xmlComplete);
saveButton.backgroundColor = 0x000000;
var data:* = FileReference(evt.target).data;
if (data is ByteArray)
{
try
{
ByteArray(data).uncompress();
}
catch(e:Error)
{
}
}
data = XML(data);
xmlText.text = data;
}
}
}
答案 0 :(得分:1)
使用Flash Player,您可以使用FileReference/save()
和load()
保存并加载本地文件。但是,这需要用户选择文件位置,而您无法控制它。
或者,您可以使用SharedObject
在本地存储数据。这可能需要用户许可,并且可以由用户(有目的或无意地)清除,例如通过他们的浏览器"清除本地数据"特征
使用AIR,您可以使用File
类来读取和编写本地文件,而无需任何用户交互,或者您可以使用EncryptedLocalStore
。
(这个问题被问了很多,如果你搜索一下你会发现更多的信息。)
修改强>
查看您发布的示例代码,它希望将XML字符串作为zlib压缩字节写入文件。在您的情况下,您希望将多个值保存到单个文件中。您可以通过简单地构造包含这些值的新XML对象来执行此操作,然后从XML对象中读取这些值。这是一个这样做的例子:
// Timeline instances
var textField1:TextField;
var textField2:TextField;
var saveBtn:SimpleButton;
var loadBtn:SimpleButton;
saveBtn.addEventListener(MouseEvent.CLICK, saveClick);
function saveClick(e:MouseEvent):void {
// Save the state of both text fields
save(textField1.text, textField2.text, "data.xml");
}
loadBtn.addEventListener(MouseEvent.CLICK, loadClick);
function loadClick(e:MouseEvent):void {
load();
}
function save(text1:String, text2:String, defaultFileName:String):void {
var xml:XML = <xml>
<text1>{text1}</text1>
<text2>{text2}</text2>
</xml>;
var file:FileReference = new FileReference();
file.save(xml, defaultFileName);
}
function load():void {
var file:FileReference = new FileReference();
file.browse([new FileFilter("XML", "*.xml")]);
file.addEventListener(Event.SELECT, loadSelect);
}
function loadSelect(e:Event):void {
var file:FileReference = e.target as FileReference;
file.addEventListener(Event.COMPLETE, loadComplete);
file.load();
}
function loadComplete(e:Event):void {
var file:FileReference = e.target as FileReference;
var xml:XML = XML(file.data.readUTFBytes(file.data.bytesAvailable));
// Assign the loaded XML text values back to the text fields
textField1.text = xml.text1;
textField2.text = xml.text2;
}