Open local txt file in flash (as3)
[ 2009-03-22 22:33:44 | Author: liuhuan ]
It might be impossible to open local file directly until Flash player 10's release. The "load" method of FileReference class provides us a way to open local file in bytearray.
There are several ways to open txt file, depending on the encoding type. If you are opening a file with utf-8 encoding, you could use:
Anyway, in most cases we used Chinese characters in our txt file. Then we might use the readMultiByte method with a "gb2312“ declaration.
Here is the full code:
Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=965
There are several ways to open txt file, depending on the encoding type. If you are opening a file with utf-8 encoding, you could use:
fileReference.data.readUTFBytes(fileReference.data.length)
Anyway, in most cases we used Chinese characters in our txt file. Then we might use the readMultiByte method with a "gb2312“ declaration.
fileReference.data.readMultiByte(fileReference.data.length,"gb2312")
Here is the full code:
import flash.text.*;
import flash.net.FileReference;
var tf:TextField = new TextField();
tf.width = 350
tf.height = 350
tf.multiline=true;
tf.border = true
addChild(tf);
var fileReference:FileReference = new FileReference();
var fileFilter:FileFilter=new FileFilter("Images","*.txt");
fileReference.addEventListener(Event.SELECT, onSelectFile);
fileReference.addEventListener(Event.COMPLETE,onFileComplete);
bBrowse.addEventListener(MouseEvent.MOUSE_DOWN, browseFile);
function onFileComplete(event:Event):void {
tf.htmlText = "opening: "+fileReference.name+"<br>"
var bytes:ByteArray=fileReference.data;
var newtext:String=fileReference.data.readMultiByte(fileReference.data.length,"gb2312");
//var newtext:String = fileReference.data.readUTFBytes(fileReference.data.length)
tf.appendText(newtext);
}
function browseFile(e:MouseEvent) {
fileReference.browse([fileFilter]);
}
function onSelectFile(event:Event):void {
fileReference.load();
}
import flash.net.FileReference;
var tf:TextField = new TextField();
tf.width = 350
tf.height = 350
tf.multiline=true;
tf.border = true
addChild(tf);
var fileReference:FileReference = new FileReference();
var fileFilter:FileFilter=new FileFilter("Images","*.txt");
fileReference.addEventListener(Event.SELECT, onSelectFile);
fileReference.addEventListener(Event.COMPLETE,onFileComplete);
bBrowse.addEventListener(MouseEvent.MOUSE_DOWN, browseFile);
function onFileComplete(event:Event):void {
tf.htmlText = "opening: "+fileReference.name+"<br>"
var bytes:ByteArray=fileReference.data;
var newtext:String=fileReference.data.readMultiByte(fileReference.data.length,"gb2312");
//var newtext:String = fileReference.data.readUTFBytes(fileReference.data.length)
tf.appendText(newtext);
}
function browseFile(e:MouseEvent) {
fileReference.browse([fileFilter]);
}
function onSelectFile(event:Event):void {
fileReference.load();
}
[Last Modified By liuhuan, at 2009-03-22 22:47:16]

There is no comment on this article.
You can't post comment on this article.