)我知道2号离这个问题还很远,告诉我如何处理类似的东西。我已经完成了类似IDE的系统,即将添加打开和保存功能。
$('#open').addEventListener('change', getTextFile, false);
function getTextFile (e) {
var files = e.target.files;
var file = files[0];
var reader = new FileReader();
reader.onload = function() {
console.log(this.result);
}
reader.readAsText(file)
}
答案 0 :(得分:0)
试试这个WORKING DEMO
function readSingleFile(evt) {
var f = evt.target.files[0];
console.log(f);
if (!f) {
alert("Failed to load file");
return;
}
if (f.name.indexOf('.txt') == -1) {
alert(f.name + " is not a valid text file.");
return;
}
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
alert( "Got the file.n"
+"name: " + f.name + "n"
+"type: " + f.type + "n"
+"size: " + f.size + " bytesn"
+ "contents: " + contents
);
document.getElementById('board').innerHTML = contents;
}
r.readAsText(f);
}
document.getElementById('fileinput').addEventListener('change', readSingleFile, false);