自动保存到使用fileReference.Save而无需保存XML作为对话框上来 [英] Autosaving to xml using fileReference.Save without having a Save As dialog come up

查看:279
本文介绍了自动保存到使用fileReference.Save而无需保存XML作为对话框上来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图挽救收集的数据在我的应用程序,以它的陪同 data.xml中称为外部XML文件。我现在用的是 fileReference.save 方法,但它只是似乎并没有做这项工作:

 函数parseXML(XMLDATA:XML):无效
{
    跟踪(分析:);
    跟踪( -  -  -  -  -  -  -  - -);
    VAR的ItemIndex:UINT = event.currentTarget.index;
    删除data.item [的ItemIndex];
    fileReference.save(数据data.xml中);
    跟踪(XMLDATA);
}
 

不幸的是,这将导致一个另存为对话框弹出询问用户,他们希望保存XML文件(当我真正想要的发生是在项目的目录中data.xml文件是没有任何自动覆盖说用户的一部分)。

我听说的FileStream 是要走的路(特别是为我准备部署这种空气的iOS),但我不知道如何实现它。

任何想法?

编辑:我已经更新了我的问题与下面的建议,但它仍然无法正常工作。该 data.xml中文件是在一个名为'RecipeMatcher2在我的桌面,即同一个目录/桌面/ RecipeMatcher2 /(我使用的是Mac OS X),但我也希望它解决的任何路径的文件将被存储在当上了iPhone。

 函数parseXML(XMLDATA:XML):无效
{
    跟踪(分析:);
    跟踪( -  -  -  -  -  -  -  - -);
    VAR的ItemIndex:UINT = event.currentTarget.index;
    文件= File.applicationStorageDirectory;
    文件= file.resolvePath(data.xml中);
    删除data.item [的ItemIndex];
    VAR writeStream:的FileStream =新的FileStream();
    writeStream.open(文件,FileMode.WRITE);
    writeStream.writeUTFBytes(data.toXMLString());
    writeStream.close();
    跟踪(XML文件保存');
}
 

我本来载入我的XML数据,像这样:

 装载机=新的URLLoader();
//听,看数据是否已经满载//
loader.addEventListener(引发Event.COMPLETE,dataLoaded);
//负载时的URLRequest方法本地XML文件//
loader.load(新的URLRequest(data.xml中));
 

和这里有我的公开瓦尔有关这个问题的:

 公共变种装载机:的URLLoader;
公共var文件:文件;
公共变种数据:XML;
公共变种项目:XMLList中;
 

解决方案

的FileReference 类是安全的Web浏览器,因为它不会让你惹用户文件,而不他的同意。

但是,你正在使用AIR可以使用文件类,它扩展了的FileReference
看看此链接,以了解它们之间的区别。

下面是如何使用的FileStream 类:

  var文件:文件=新的文件()与resolvePath(MYFILE.TXT);

VAR流:的FileStream =新的FileStream();
stream.open(文件,FileMode.WRITE);
stream.writeUTF(世界,你好!);
stream.close();
 

,你可以阅读更多关于<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html"相对=nofollow> FileStream类这里。


修改:看你当前编辑的问题,我建议你用文件 / 的FileReference ,类似如下:

 私人var文件:文件=新的文件();

私有函数的loadFile():无效
{
    file.addEventListener(引发Event.COMPLETE,onFileComplete);
    file.resolvePath(data.xml中); //或任何你的文件的位置
    file.load();
}

私有函数了saveFile():无效
{
    VAR writeStream:的FileStream =新的FileStream();
    / *
     * FileMode.WRITE将截断(空)上打开的文件。
     * FileMode.APPEND将打开文件原样写在它的结束。
     * /
    writeStream.open(文件,FileMode.WRITE);
    writeStream.writeBytes(数据); //可以使用任何其他写入方法
    writeStream.close();
}

私有函数onFileComplete(事件:事件):无效
{
    VAR的xml:XML = XML(file.data); //你的XML是在这里!
    parseXML(XML); //这里做你的东西
}
 

正如你所看到的,一旦我们有了一个指向该文件在文件系统中,我们可以读/写数据从/给它自由。

I am trying to save data gathered from within my app to it's accompanying external xml file called data.xml. I am using the fileReference.save method but it just doesn't seem to be doing the job:

function parseXML(xmlData:XML):void
{
    trace("Parsing:");
    trace("-----------------");
    var itemIndex:uint = event.currentTarget.index;
    delete data.item[itemIndex];
    fileReference.save(data, "data.xml");
    trace(xmlData);
}

Unfortunately, this causes a 'Save As' dialog box to pop up asking the user where they would like to save the xml file (when what I really want to happen is for the data.xml file in the project's directory to be overwritten automatically without any say on the part of the user).

I have heard that FileStream is the way to go (especially being I am going to deploy this on Air for iOS), but I don't know how to implement it.

Any ideas?

EDIT: I have updated my question with the suggestion below, but it still doesn't work. The data.xml file is in the same directory called 'RecipeMatcher2' on my Desktop i.e. /Desktop/RecipeMatcher2/ (I am using Mac OS X), but I also want it to resolve to whatever path the file will be stored in when on the iPhone.

function parseXML(xmlData:XML):void
{
    trace("Parsing:");
    trace("-----------------");
    var itemIndex:uint = event.currentTarget.index;
    file = File.applicationStorageDirectory;
    file = file.resolvePath("data.xml");
    delete data.item[itemIndex];
    var writeStream:FileStream = new FileStream();
    writeStream.open(file, FileMode.WRITE);
    writeStream.writeUTFBytes(data.toXMLString());
    writeStream.close();
    trace('xml file saved');
}

I originally load my xml data like so:

loader = new URLLoader();
// listens to see if data has fully loaded//
loader.addEventListener(Event.COMPLETE, dataLoaded);
//loads local xml file with URLrequest method//
loader.load(new URLRequest("data.xml"));

and here are my public vars relevant to this question:

public var loader:URLLoader;
public var file:File;
public var data:XML;
public var items:XMLList;

解决方案

The FileReference class is safe for web browsers because it doesn't let you mess with users files without his consent.

But, as you are using AIR you can use the File class that extends the FileReference.
Take a look at this link to understand the difference between them.

Here's how you can use the FileStream class:

var file:File = new File().resolvePath("myFile.txt");

var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTF("Hello world!");
stream.close();

And you can read more about the FileStream class here.


EDIT: Looking at your current edited question I'd recommend you to load the xml data with a File/FileReference, something like the following:

private var file:File = new File();

private function loadFile():void 
{
    file.addEventListener(Event.COMPLETE, onFileComplete);
    file.resolvePath("data.xml"); // Or whatever your file is located
    file.load();
}

private function saveFile():void 
{
    var writeStream:FileStream = new FileStream();
    /*
     * FileMode.WRITE will truncate (empty) the file on open.
     * FileMode.APPEND will open the file "as is" and write in the end of it.
     */
    writeStream.open(file, FileMode.WRITE);
    writeStream.writeBytes(data); // Any other write method can be used
    writeStream.close();
}

private function onFileComplete(event:Event):void 
{
    var xml:XML = XML(file.data); // Your XML is here!
    parseXML(xml); // Do your stuff here
}

As you can see, once we get a reference to the file in the file system we can read/write data from/to it freely.

这篇关于自动保存到使用fileReference.Save而无需保存XML作为对话框上来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆