<input type='file' id='file' name='file'>
获取了我的文件参考
console.log("Have now created a new file reader and it looks like this..." + reader);
reader.onload = function() {
var contents = event.target.result;
console.log("File contents: " + contents );
};
reader.onerror = function(event) {
console.error("File could not be read! Code " + event.target.error.code);
};
reader.readAsText(file);
}, false);
我做错了什么?
谢谢,
J.Wells
答案 0 :(得分:3)
我做错了什么?
您似乎忘记了event
处理程序的onload
参数。您也可以使用event.target
。
reader
另外,在小提琴中,你以非常奇怪的方式创建FileReader
。您可能需要阅读简介Using files from web applications at MDN。
document.getElementById("file").addEventListener("change", function(e) {
var file = e.target.files[0],
reader = new FileReader();
console.log("Have now created a new file reader and it looks like this..." + reader);
reader.onload = function(event) {
// ^^^^^
var contents = event.target.result;
console.log("File contents: " + contents );
};
reader.onerror = function(event) {
console.error("File could not be read! Code " + event.target.error.code);
};
reader.readAsText(file);
}, false);