HTML5 File API: How to see the result of readAsText()

后端 未结 4 730
野趣味
野趣味 2020-12-08 04:30

When readAsText() function is completed the result is stored in .result

How do I see if the content of the file read are correct in .

4条回答
  •  悲&欢浪女
    2020-12-08 04:54

    This took me like 300 hours to figure out even after reading the documentation and examples online...

    Here's some actual, working code:

    let fileReader = new FileReader();
    fileReader.onload = function(event) {
        alert(fileReader.result);
    };
    inputElement.onchange = function(event) {
        fileReader.readAsText(event.target.files[0]);
    };
    

    Also, FYI:

    FileReader.onabort A handler for the abort event. This event is triggered each time the reading operation is aborted.

    FileReader.onerror A handler for the error event. This event is triggered each time the reading operation encounter an error.

    FileReader.onload A handler for the load event. This event is triggered each time the reading operation is successfully completed.

提交回复
热议问题