如何将嵌入文件写入磁盘?

时间:2014-07-30 09:18:22

标签: actionscript-3 flex air

我的目标是将嵌入资源写入文件

[Embed(source="Embed/viewer.js", mimeType="application/octet-stream")]
private var viewer_js:Class;

[Embed(source="Embed/viewer2.js", mimeType="application/octet-stream")]
private var viewer2_js:Class;


private function writeAssetToFile(embed:Class, targetFileName:String):void {
    var file:File = File.desktopDirectory.resolvePath(targetFileName);
    var fs:FileStream = new FileStream();
    fs.open(file, FileMode.WRITE);

    fs.writeBytes(embed as ByteArray); // error here
    fs.close(); 
}

例如:

 writeAssetToFile(viewer_js,"viewer.js");

fs.writeBytes(嵌入为ByteArray);是造成问题的一条线。

问候

1 个答案:

答案 0 :(得分:2)

你只需编写一个类的实例,如下所示:

private function writeAssetToFile(embed:Class, targetFileName:String):void {
    var file:File = File.desktopDirectory.resolvePath(targetFileName);
    var fs:FileStream = new FileStream();
    fs.open(file, FileMode.WRITE);
    fs.writeBytes(new embed() as ByteArray); // here, should fix
    fs.close(); 
}

诀窍是,嵌入application/octet-stream的任何内容都被实例化为ByteArray后代,因此您不需要再做任何事情。