gpt4 book ai didi

javascript - console.log() 本地上传的csv文件

转载 作者:行者123 更新时间:2023-12-01 01:17:35 28 4
gpt4 key购买 nike

这是我代码中的文件上传处理程序。

onFileUpload() {
const file = document.querySelector('input[type=file]').files[0];

const reader = new FileReader();
console.log('contents of file:', reader.readAsText(file));

this.props.getFile(file); // an action
}

此代码片段记录undefined到控制台。缺少什么?

更新 1。render() React 组件中的方法。 onFileUpload()方法在构造函数中绑定(bind)为 this.onFileUpload = this.onFileUpload.bind(this) .

render() {
return (
<div>
...
<input type='file' id='files' className='file-input-hidden' onChange={this.onFileUpload} />
</div>
);
}

最佳答案

onFileUpload() {
const file = document.querySelector('input[type=file]').files[0];

const reader = new FileReader()
reader.onload = event => console.log(event.target.result)
reader.onerror = error => throw(error)
reader.readAsText(file)

this.props.getFile(file); // an action
}

您只需添加一个监听器来进行日志记录,而不是同步进行

关于javascript - console.log() 本地上传的csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54597976/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com