React是否可以自动从文本/属性文件中读取数据并在浏览器上显示



例如,我有一个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");

最新更新