如何使用Flash来实现本地存储
我在5月份的一篇文章里列举了一些本地存储解决方案,包括常见的Cookie、UserData和不是那么常见的globalStorage、Database Storage。文章最后提到了另外两种解决方案:Google Gear和Flash,当时因为觉得用不上就没仔细研究。但实际应用中,那篇文章列出的方案还是不能满足项目的需求。这篇文章就讲一下如何使用Flash来实现本地存储,以及该方案使用的场合。
拿IE6来说,如果要在本地大量存放数据,Cookie因为存放内容太少、浪费用户带宽首先就应该被淘汰掉。UserData在大多数情况下能满足需求,但是它也有一个致命的弱点:只能读取同目录存储,也就是a目录下一个文件不能读取b目录下两一个文件存放的数据。MSDN说明如下:
Security Alert For security reasons, a UserData store is available only in the same directory and with the same protocol used to persist the store. 全文>>
也就是说如果项目中要跨文件夹操作本地存储,UserData也必须被淘汰了。IE不支持Database Storage,IE8才增加了对globalStorage的支持,这个时候Flash就派上用场了。
首先,打开Flash,建立一个工程,编写get、set、remove三个方法并提供给外部程序调用:
var sobj = SharedObject.getLocal(name);
sobj.data[key] = val;
sobj.flush();
}
function get(name, key) {
var sobj = SharedObject.getLocal(name);
return(sobj.data[key]);
}
function remove(name, key) {
var sobj = SharedObject.getLocal(name);
delete sobj.data[key];
sobj.flush();
}
flash.external.ExternalInterface.addCallback("set", this, set);
flash.external.ExternalInterface.addCallback("get", this, get);
flash.external.ExternalInterface.addCallback("remove", this, remove);
其中,核心功能是依靠SharedObject来实现的。对于这个对象,Adobe官方说明如下:
Shared objects are quite powerful: they offer real-time data sharing between objects that are persistent on the local location. You can think of local shared objects as “cookies.”
You can use local shared objects to maintain local persistence. This is the simplest way to use a shared object. For example, you can call SharedObject.getLocal to create a shared object, such as a calculator with memory, in the player. Because the shared object is locally persistent, Flash saves its data attributes on the user’s machine when the movie ends. The next time the movie runs, the calculator contains the values it had when the movie ended. Alternatively, if you set the shared object’s properties to null before the movie ends, the calculator opens without any prior values the next time the movie runs. 全文>>
然后用把编译得到的swf文件加到页面中,利用js来调用flash中对应的三个方法就OK了,具体方法都写在演示文件里,就懒得贴出来了。演示地址在这里(请用IE浏览器打开)

胡子 said(Sep 15, 08 at 11:55 下午):
为什么你这个只支持IE?什么原因
admin said(Sep 16, 08 at 1:03 下午):
也许可以吧,因为在firefox下有globalStorage,safari下可以用本地Database,所以没仔细研究下去~