package {
import flash.text.TextField;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.net.FileReference;
import flash.display.MovieClip;
/**
* @author jh7086
*/
public class Test extends MovieClip {
private var ref : FileReference;
private var btn_browse : MovieClip;
private var textField : TextField;
public function Test() {
ref = new FileReference();
ref.addEventListener(Event.SELECT, file_select);
ref.addEventListener(Event.COMPLETE, file_complete);
btn_browse = this["_btn_browse"] as MovieClip;
btn_browse.buttonMode = true;
btn_browse.addEventListener(MouseEvent.CLICK, btn_browse_click);
textField = this["_text"] as TextField;
}
private function btn_browse_click(event : MouseEvent) : void {
ref.browse();
}
private function file_select(event : Event) : void {
ref.load();
}
private function file_complete(event : Event) : void {
//解析字节数组
var data : ByteArray = ref.data;
var type : String = getFileType(data);
var length : uint = data.length;
var result : String = "";
switch(type) {
case "ANSI" :
data.position = 0;
result = data.readMultiByte(length-data.position, "gb2312");
break;
case "Unicode" :
case "Unicode big endian" :
case "UTF-8" :
result=data.toString();
break;
}
textField.text = type + "\n\n" + result;
textField.scrollV=1;
}
//根据文件头信息判断文本文件编码类型;
private function getFileType(fileData : ByteArray) : String {
var b0 : int = fileData.readUnsignedByte();
var b1 : int = fileData.readUnsignedByte();
var fileType : String = "ANSI";
if(b0 == 0xFF && b1 == 0xFE) {
fileType = "Unicode";
}else if(b0 == 0xFE && b1 == 0xFF) {
fileType = "Unicode big endian";
}else if(b0 == 0xEF && b1 == 0xBB) {
fileType = "UTF-8";
}
return fileType;
}
}
}
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}