將 AIR 檔下載至使用者的電腦

若要使用 Updater 類別,使用者或應用程式必須先以本機方式將 AIR 檔儲存在使用者的電腦上。

備註: AIR 1.5 所具備的更新架構可協助開發人員賦予 AIR 應用程式良好的更新能力。使用這個架構可能比直接使用 Update 類別的 update() 方法簡單得多。如需詳細資訊,請參閱 使用更新架構

下列程式碼會從 URL (http://example.com/air/updates/Sample_App_v2.air) 讀取 AIR 檔,並將 AIR 檔儲存在應用程式儲存目錄中。

ActionScript 範例:

var urlString:String = "http://example.com/air/updates/Sample_App_v2.air"; 
var urlReq:URLRequest = new URLRequest(urlString); 
var urlStream:URLStream = new URLStream(); 
var fileData:ByteArray = new ByteArray(); 
urlStream.addEventListener(Event.COMPLETE, loaded); 
urlStream.load(urlReq); 
 
function loaded(event:Event):void { 
    urlStream.readBytes(fileData, 0, urlStream.bytesAvailable); 
    writeAirFile(); 
} 
 
function writeAirFile():void { 
    var file:File = File.applicationStorageDirectory.resolvePath("My App v2.air"); 
    var fileStream:FileStream = new FileStream(); 
    fileStream.open(file, FileMode.WRITE); 
    fileStream.writeBytes(fileData, 0, fileData.length); 
    fileStream.close(); 
    trace("The AIR file is written."); 
}

JavaScript 範例:

var urlString = "http://example.com/air/updates/Sample_App_v2.air"; 
var urlReq = new air.URLRequest(urlString); 
var urlStream = new air.URLStream(); 
var fileData = new air.ByteArray(); 
urlStream.addEventListener(air.Event.COMPLETE, loaded); 
urlStream.load(urlReq); 
 
function loaded(event) { 
    urlStream.readBytes(fileData, 0, urlStream.bytesAvailable); 
    writeAirFile(); 
} 
 
function writeAirFile() { 
    var file = air.File.desktopDirectory.resolvePath("My App v2.air"); 
    var fileStream = new air.FileStream(); 
    fileStream.open(file, air.FileMode.WRITE); 
    fileStream.writeBytes(fileData, 0, fileData.length); 
    fileStream.close(); 
    trace("The AIR file is written."); 
}

如需詳細資訊,請參閱: