从JavaScript中的HTML文件解析文件

时间:2018-11-02 06:18:35

标签: javascript html file parsing filereader

我正在尝试从JavaScript中的html页面解析csv文件。

在我的HTML页面中,我有以下代码,为我提供了一个文件输入区域,以及一个执行我的JS脚本的按钮:

 <input type="file" id="csvFile" name="files[]"><br>
  <button onclick="loadCSV()">load csv</button>

在Javascript中,我在访问文件数据时遇到困难。

我有以下代码:

function loadCSV() {
    var fileUpload=document.getElementById("csvFile").files[0];
    var reader = new FileReader();
    reader.readAsText(fileUpload/*, "UTF-8"*/);
    console.log(reader)

这将在控制台中产生以下数据:

FileReader {readyState: 1, result: "", error: null, onloadstart: null, onprogress: null, …}
    error: null
    onabort: null
    onerror: null
    onload: null
    onloadend: null
    onloadstart: null
    onprogress: null
    readyState: 2
    result: "1↵00:00:00,200 --> 00:00:03,700↵Powerful Presentations: Simply Stated...↵↵2↵00:00:03,800..
    __proto__: FileReader

它显示结果中存储的文件数据,但是,如果我尝试访问reader.result,则返回的任何内容都没有,控制台中只有空白行。

我应该使用什么方法来访问文件数据?

0 个答案:

没有答案