在flex中輸出文件到本地目前我用到兩種方法,分別是File和FielRefreence
例子:
var exportString:String = "這就是一個(gè)測(cè)試"
1、File輸出
復(fù)制代碼 代碼如下:
var ff:File = File.desktopDirectory.resolvePath("Progress.CSV");
ff.addEventListener(Event.SELECT,exportCSVSelectedHandler);
ff.browseForSave("選擇路徑");
private function exportCSVSelectedHandler(event:Event):void
{
var dir:String = event.target.nativePath;
var writeStream:FileStream = new FileStream();
writeStream.open(new File(dir),FileMode.WRITE);
var content:String = "jhfdklahflaksjdhflkasjhflaskjhlf\n";
writeStream.writeMultiByte(content,'utf-8');
writeStream.close();
}
2、FileReference 輸出
復(fù)制代碼 代碼如下:
var content:String = "jhfdklahflaksjdhflkasjhflaskjhlf\n";
var f:FileReference = new FileReference();
f.save(content,"1.csv");
File和FileReference兩者的差別在于File可以指定文檔的編碼格式,而FileReference使用起來相對(duì)簡(jiǎn)單