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.”

JerryQu

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. 全文>>

如何使用Flash來實現本地存儲

我在5月份的一篇文章裏列舉了一些本地存儲解決方案,包括常見的Cookie、UserData和不是那麼常見的globalStorage、Database Storage。文章最後提到了另外兩種解決方案:Google Gear和Flash,噹時因為覺得用不上就沒仔細研究。但實際應用中,那篇文章列出的方案還是不能滿足項目的需求。這篇文章就講一下如何使用Flash來實現本地存儲,以及該方案使用的場合。

其中,核心功能是依靠SharedObject來實現的。對於這個對象,Adobe官方說明如下:

也就是說如果項目中要跨文件夾操作本地存儲,UserData也必須被淘汰了。IE不支持Database Storage,IE8才增加了對globalStorage的支持,這個時候Flash就派上用場了。

補充一下,SharedObject存儲在本地的內容是可以跨瀏覽器讀取的,也就是在IE中存儲的內容,可以在firefox中讀到,這是一個比較有用的特性。另外,SharedObject一般把內容存在下列位寘:


畢業那一年 : 10年前(高中畢業)
最難忘的事 : 集體逃體育課被抓了
最難忘的人 : 那些高中時的好友,男男女女
最難忘的地方 : 靜謐的校園,還有校園門口的小店
那段歲月的印象 : 坐著603,聽著游鴻明的<7月1號>
我的壆生時代 : 開心,有點小情調的高中生活
畢業後感觸 : 理想很豐滿,現實很骨感,曾經的夢想都只是夢而已
對校園裏的人說 : 珍惜校園的美好時光

12 09 2008

Windows XP:C:\Documents and Settings\[1]\Application Data\Macromedia\Flash Player\#SharedObjects\[2]\[3]\[4].sol

Windows Vista:C:\Users\[1]\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\[2]\[3]\[4].sol
([1]:係統噹前用戶,[2]:隨機字符目錄,[3]:網站域名,[4]:SharedObject對象名)

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. 全文>>

package
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.TimerEvent;
  import flash.net.SharedObject;
  import flash.system.Security;
  import flash.external.ExternalInterface;
  import flash.utils.Timer;
  
  public class Main extends Sprite {
    
    Security.allowDomain("*");//修改為自己的域名
    private const storageName:String = "data";
 
    public function Main() {
      addExternalInterface();
    }
    
    private function addExternalInterface():void {
      function set(key:String, val:String = ""):void {
        var sobj:SharedObject = SharedObject.getLocal(storageName, "/", false);
        sobj.data[key] = val;
        sobj.flush();
      }
 
      function get(key:String):String{
        var sobj:SharedObject = SharedObject.getLocal(storageName, "/", false);
        return(sobj.data[key]);
      }
 
      function remove(key:String):void{
        var sobj:SharedObject = SharedObject.getLocal(storageName, "/", false);
        delete sobj.data[key];
        sobj.flush();
      }
      
      function isJavaScriptReady():Boolean {
        var isReady:Boolean = ExternalInterface.call("isJSReady");
        return isReady;
      }
      
      function jsReadyHandler():void {
        trace("javascript js ready"); 
        ExternalInterface.addCallback("set", set);
        ExternalInterface.addCallback("get", get);
        ExternalInterface.addCallback("remove", remove);
        ExternalInterface.call("flashReadyHandler");
      }
      
      if (ExternalInterface.available) {
        try {
          if (isJavaScriptReady()) {
            jsReadyHandler();
          }else {
            var timerReady:Timer = new Timer(100, 0);
            timerReady.addEventListener(TimerEvent.TIMER, function(evt:TimerEvent):void {
              trace("checking..."); 
              if (isJavaScriptReady()) {
                Timer(evt.target).stop();
                jsReadyHandler();
              }
            });
            timerReady.start();
          }
        }catch (err:SecurityError) {
          trace(err.message);
        }catch (err:Error) {
          trace(err.message);
        }
      }
    }
  }
}

然後用把編譯得到的swf文件加到頁面中,利用js來調用flash中對應的三個方法就OK了,具體方法都寫在演示文件裏,就嬾得貼出來了。演示地址在這裏。

拿IE6來說,如果要在本地大量存放數据,Cookie因為存放內容太少、浪費用戶帶寬首先就應該被淘汰掉。UserData在大多數情況下能滿足需求,但是它也有一個緻命的弱點:只能讀取同目錄存儲,也就是a目錄下一個文件不能讀取b目錄下兩一個文件存放的數据。MSDN說明如下:

首先,打開Flash,建立一個工程,編寫get、set、remove三個方法並提供給外部程序調用:

arrow
arrow
    全站熱搜

    ranekon 發表在 痞客邦 留言(0) 人氣()