javascript - 尝试使用 readAsText 时出现 TypeMismatchError
问题描述
我试图在页面启动时加载一个文本文件,但它导致 Reader.readAsText(text.files[0]) 的 TypeMismatchError;
function mm(){
var text=document.getElementById("aspect");
const reader=new FileReader();
reader.onload= function(){
//var lines = this.result.split('\n');
console.log(reader.result);};
reader.readAsText(text.files[0]);}
window.addEventListener("load",mm,false);
<body>
<input id="aspect" type="file" value="aspects.txt" style="display:none;" />
</body>
我希望打印文件内容,但我收到了错误消息
解决方案
您想mm
在选择文件而不是在窗口加载时调用,请在文件输入上添加更改事件处理程序。
function mm(){
var text=document.getElementById("aspect");
const reader=new FileReader();
reader.onload= function(){
//var lines = this.result.split('\n');
console.log(reader.result);};
reader.readAsText(text.files[0]);
}
<body>
<input id="aspect" type="file" value="aspects.txt" onchange="mm()" style="display:none;" />
</body>
推荐阅读
- html - 添加后备图片 {HTML&CSS}
- swift - 快速通过BLE蓝牙接收语音
- mongodb - MongoDB:如何获取给定 2 个日期之间的时间差(以毫秒为单位)
- python - 如何在 spyder 中安装“pandas.Series”
- c++ - 并行化累积概率分布
- python - 如何匹配恰好 1 个或 2 个换行符或换行符的后视?
- rabbitmq - RabbitMQ/AMQP - 如何从外部安全地连接消费者/发布者?
- binance - 访问熊猫数据框中的比特币优势
- php - PHP cURL 例程返回警告消息
- arduino - 如何在 Arduino nano BLE 上查询板子序列号