使用JavaScript读取INI文件
我不太熟悉JavaScript,目标是读取INI文件并获取值。示例INI文件包含以下项使用JavaScript读取INI文件,javascript,html,Javascript,Html,我不太熟悉JavaScript,目标是读取INI文件并获取值。示例INI文件包含以下项 [Tutor] Name = Ajast Course = Victor [Order] Title = MAX New Ring = 12990 我想阅读上面的内容并将其存储到类似于map的键值对中 例如 Key = Name & value = Ajast 像那样 到目前为止,我所能做的就是,拿出一个HTML/JS,让用户选择一个INI文件,然后读取并在页面中显示它的内容 <html&
[Tutor]
Name = Ajast
Course = Victor
[Order]
Title = MAX New
Ring = 12990
我想阅读上面的内容并将其存储到类似于map的键值对中
例如
Key = Name & value = Ajast
像那样
到目前为止,我所能做的就是,拿出一个HTML/JS,让用户选择一个INI文件,然后读取并在页面中显示它的内容
<html>
<head>
<title>reading file</title>
<script type="text/javascript">
var reader = new FileReader();
function readText(that){
if(that.files && that.files[0]){
var reader = new FileReader();
reader.onload = function (e) {
var output=e.target.result;
document.getElementById('main').innerHTML= output;
};//end onload()
reader.readAsText(that.files[0]);
}//end if html5 filelist support
}
</script>
</head>
<body>
<input type="file" onchange='readText(this)' />
<div id="main"></div>
</body>
</html>
读取文件
var reader=new FileReader();
函数readText(即){
if(that.files&&that.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
var输出=e.target.result;
document.getElementById('main').innerHTML=output;
};//结束onload()
reader.readAsText(即.files[0]);
}//如果html5文件列表支持,则结束
}
我希望上面的任务可以用JavaScript实现。可以吗?根据答案-
您可以使用脚本在客户端使用javaScript读取*.ini数据:)
仅供参考,一旦您获得15分,您将能够将问题标记为其他问题的重复项,这里似乎是这样的。@hoppeduppen但谢谢你,但我只有6个名声=D@missnefrat-您能否提供一个包含HTML的工作示例,让用户从客户端选择一个INI文件,然后将INI文件内容作为键值对进行解析和显示?@donguy76您可以使用我之前发布的代码轻松完成此操作!你怎么回事啊?
/*
Read a ini file
[Section1]
Param1=value1
[Section2]
Param2=value2
*/
var fs = require("fs");
function parseINIString(data){
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([^=]+?)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
var value = {};
var lines = data.split(/[\r\n]+/);
var section = null;
lines.forEach(function(line){
if(regex.comment.test(line)){
return;
}else if(regex.param.test(line)){
var match = line.match(regex.param);
if(section){
value[section][match[1]] = match[2];
}else{
value[match[1]] = match[2];
}
}else if(regex.section.test(line)){
var match = line.match(regex.section);
value[match[1]] = {};
section = match[1];
}else if(line.length == 0 && section){
section = null;
};
});
return value;
}
try {
var data = fs.readFileSync('C:\\data\\data.dat', 'utf8');
var javascript_ini = parseINIString(data);
console.log(javascript_ini['Section1']);
}
catch(e) {
console.log(e);
}