必须将编译器版本设为10.0.0以上,设置如下
FlexBuilder:
Flex项目- 右键 - Properties -flex compiler - required flash player version
Flex类库项目- 右键 - Properties - flex library compiler - additional compiler aguments:
--target-player=10.0.0
命令行:
mxmlc -target-player 10.0.0
compc -target-player 10.0.0
---
private var fr:FileReference;
//called when the user clicks the load file button
private function saveFile(data:Object, fileName:String):void {
//create the FileReference instance
fr=new FileReference();
//listen for the file has been saved
fr.addEventListener(Event.COMPLETE, onFileSave);
//listen for when then cancel out of the save dialog
fr.addEventListener(Event.CANCEL,onCancel);
//listen for any errors that occur while writing the file
fr.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);
//open a native save file dialog, using the default file name
fr.save(data, fileName)
}
/***** File Save Event Handlers ******/
//called once the file has been saved
private function onFileSave(e:Event):void {
trace("File Saved");
}
private function onCancel(e:Event):void {
trace("File save select canceled.");
fr=null;
}
private function onSaveError(e:IOErrorEvent):void {
trace("Error Saving File : " + e.text);
fr=null;
}
private function export():void {
var csv:CSV=new CSV();
csv.embededHeader=false
csv.header=['label 1', 'label 2', '姓名', 'label 4']
csv.addRecordSet( ['1','b','c','k'] )
csv.addRecordSet( ['0','b','g','d'], -1 )
csv.encode()
trace( 'Is string: ' + (csv.data is String) + '\r' + csv.data );
var g:String=csv.data + "";
//解决中文问题
var b:ByteArray=new ByteArray();
b.writeMultiByte(g, "GBK");
trace("length = " + b.length);
trace("bytesAvailable = " + b.bytesAvailable);
trace("position = " + b.position);
b.position=0;
saveFile(b, "Export.csv");
}
封装了一个工具类
package com.nsn.utils {
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.FileReference;
public class FileUtils {
public function FileUtils() {
}
private var fr:FileReference;
//保存成功回调函数
private var onDoneCallback:Function;
//IOError 回调函数
private var onErrorCallback:Function;
//取消保存回调函数
private var onCancelCallback:Function;
/**
* 保存文件
* @param data 要保存的数据
* @param fileName 文件名称
* @param onDone 保存成功回调函数
* @param onError IOError 回调函数
* @param onCancel 取消保存回调函数
*
* 示例:
* <code>
* FileUtils.save(...);
* </code>
*/
public static function save(data:Object, fileName:String, onDone:Function=null, onError:Function=null, onCancel:Function=null):void {
new FileUtils().saveFile(data, fileName, onDone, onError, onCancel);
}
//called when the user clicks the load file button
public function saveFile(data:Object, fileName:String, onDone:Function=null, onError:Function=null, onCancel:Function=null):void {
onDoneCallback = onDone;
onErrorCallback = onError;
onCancelCallback = onCancel;
//create the FileReference instance
fr=new FileReference();
//listen for the file has been saved
fr.addEventListener(Event.COMPLETE, onFileSave);
//listen for when then cancel out of the save dialog
fr.addEventListener(Event.CANCEL,onCancel);
//listen for any errors that occur while writing the file
fr.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);
//open a native save file dialog, using the default file name
fr.save(data, fileName)
}
/***** File Save Event Handlers ******/
//called once the file has been saved
private function onFileSave(e:Event):void {
trace("File Saved");
fr=null;
if(onDoneCallback != null){
onDoneCallback(e);
}
onDoneCallback = null;
}
//called if the user cancels out of the file save dialog
private function onCancel(e:Event):void {
trace("File save select canceled.");
fr=null;
if(onCancelCallback != null){
onCancelCallback(e);
}
onCancelCallback = null;
}
//called if an error occurs while saving the file
private function onSaveError(e:IOErrorEvent):void {
trace("Error Saving File : " + e.text);
fr=null;
if(onErrorCallback != null){
onErrorCallback(e);
}
onErrorCallback = null;
}
}
}