Is there a way to read local files in ActionScript 3 “silently”'?

痴心易碎 提交于 2019-12-06 05:20:29

Assuming since this runs on the desktop you are using AIR. If so, you can use the standard AIR file APIs like so (this assumes the file lives in the user directory):

var file:File = File.userDirectory.resolvePath("myfilename.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var str:String = fileStream.readUTFBytes(file.size);
fileStream.close();

The above also assumes the text file is UTF-8 encoded. If not, there are other read methods in the FileStream class.

AIR also has access to other directories, of course. For instance, to get a file in the root of the C: drive on windows:

var file:File = new File("C:/mytextfile.txt");

If you are not using AIR, you can compile your SWF with "local-with-filesystem" permissions. You can then use URLLoader to load local files. (You can also designate a SWF as trusted, and then you can access both the filesystem and the network.)

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