使用 Javascript 同步读取本地文件
Synchronous Read of Local File using Javascript
我有一个数据文件,以换行符分隔,包含各种名称,我想将其放入数组中以在 JavaScript 中进行处理。这组名称最终需要排序,但现在我正在处理实际的文件加载过程。这是我现在的代码。
var nameslist = document.baseURI.split('.'); nameslist.pop(); nameslist = nameslist.join('.') + ".dat";
console.log("Attempting to read from file: " + nameslist);
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open("GET", nameslist, false);
reader.send(null);
nameslist = reader.responseText.split('\n');
nameslist.pop();
console.log("Elements read: " + nameslist.length);
正如预期的那样,数据文件由三个名称组成(对于测试用例),我在控制台日志中得到以下结果...
"Attempting to read from file: file:///home/username/Desktop/test/test.dat" test.js:11
"Elements read: 3" test.js:19
问题是我的日志中也出现了以下警告和错误,我想将其消除(因为简单地忽略它可能会导致以后出现问题)...
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/ test.js:14
syntax error test.dat:1
根据我对折旧警告的研究,似乎 Javascript 作为一种语言不喜欢同步操作,因为它会减慢脚本的 运行ning 速度。然而,在这种情况下,我需要在脚本的其余部分有意义 运行 之前加载所有内容。我知道我可以重构它以将脚本的其余部分放在一个函数中,该函数在从 responseText 中提取数据后调用,并且只需在 reader.open 行上使用 true,但是有没有更好的方法来获取 JavaScript 同步加载有问题的数据文件,而不必开始制作只会被调用一次的 main() 函数?
至于指向数据文件第一行的流氓错误,坦率地说,我很难过。关于可能导致此行为的原因有什么想法吗?
提前感谢我从社区获得的任何答案。
重写代码解决如下:
var nameslist = document.baseURI.split('.'); nameslist.pop(); nameslist = nameslist.join('.') + ".dat";
console.log("Attempting to read from file: " + nameslist);
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open("GET", nameslist);
reader.onloadend = main;
reader.responseType = "text";
reader.send();
function main()
{
nameslist = reader.responseText.split('\n'); nameslist = nameslist.filter(function(n){return n;}).sort();
console.log("Elements read: " + nameslist.length);
// Additional code to be run after the load is complete goes in this block, starting at this line.
}
我有一个数据文件,以换行符分隔,包含各种名称,我想将其放入数组中以在 JavaScript 中进行处理。这组名称最终需要排序,但现在我正在处理实际的文件加载过程。这是我现在的代码。
var nameslist = document.baseURI.split('.'); nameslist.pop(); nameslist = nameslist.join('.') + ".dat";
console.log("Attempting to read from file: " + nameslist);
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open("GET", nameslist, false);
reader.send(null);
nameslist = reader.responseText.split('\n');
nameslist.pop();
console.log("Elements read: " + nameslist.length);
正如预期的那样,数据文件由三个名称组成(对于测试用例),我在控制台日志中得到以下结果...
"Attempting to read from file: file:///home/username/Desktop/test/test.dat" test.js:11
"Elements read: 3" test.js:19
问题是我的日志中也出现了以下警告和错误,我想将其消除(因为简单地忽略它可能会导致以后出现问题)...
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/ test.js:14
syntax error test.dat:1
根据我对折旧警告的研究,似乎 Javascript 作为一种语言不喜欢同步操作,因为它会减慢脚本的 运行ning 速度。然而,在这种情况下,我需要在脚本的其余部分有意义 运行 之前加载所有内容。我知道我可以重构它以将脚本的其余部分放在一个函数中,该函数在从 responseText 中提取数据后调用,并且只需在 reader.open 行上使用 true,但是有没有更好的方法来获取 JavaScript 同步加载有问题的数据文件,而不必开始制作只会被调用一次的 main() 函数?
至于指向数据文件第一行的流氓错误,坦率地说,我很难过。关于可能导致此行为的原因有什么想法吗?
提前感谢我从社区获得的任何答案。
重写代码解决如下:
var nameslist = document.baseURI.split('.'); nameslist.pop(); nameslist = nameslist.join('.') + ".dat";
console.log("Attempting to read from file: " + nameslist);
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open("GET", nameslist);
reader.onloadend = main;
reader.responseType = "text";
reader.send();
function main()
{
nameslist = reader.responseText.split('\n'); nameslist = nameslist.filter(function(n){return n;}).sort();
console.log("Elements read: " + nameslist.length);
// Additional code to be run after the load is complete goes in this block, starting at this line.
}