例如,我有一个Demo.txt
文件,内容为";版本:1.0";我想在UI上显示这个。我尝试过fileSystem方法,但在Firefox上得到了错误readFile/readSyncFile is not a function
。可能,fs不是一种合适的方法。
PS-我避免在运行时浏览文件。
试试这个:
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}
这样使用:
readTextFile("file:///C:/your/path/to/file.txt");