Is it possible to load a local file without having to ask the user to browse to it first in an AIR Application?

浪子不回头ぞ 提交于 2019-12-13 05:32:43

问题


I'm writing a small application for myself and instead of using a database I'd like to just use Excel to store the small amount of data I have on my local file system. I want to be able to load that data without having to use the typical FileReference browse() method as it would just be annoying to do every time I use the application.

The code below seems to find the file fine as the file.exists method below and most of the other attributes seem to be correct but the file.data is always null.

I'm guessing this is a security issue and that's why I'm running into this problem but I thought I'd ask and see if there is in fact a way around this problem.

var file:File = new File(fullPath + "\\" + currentFolder + ".txt");
if(file.exists) {
     var byteArray:ByteArray = file.data;
}

回答1:


If you want to read the content of a file, use the following code:

var stream:FileStream = new FileStream();
stream.open("some path here", FileMode.READ);
var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
trace(fileData);

The data property is inherited from FileReference class and it will be populated only after a load call (see this link).




回答2:


You're close, you just need to combine that with a FileStream object

var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
trace(str);

more info here



来源:https://stackoverflow.com/questions/6786236/is-it-possible-to-load-a-local-file-without-having-to-ask-the-user-to-browse-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!