FileReader to String

风格不统一 提交于 2020-01-11 05:45:12

问题


Consider the following code

function readSingleFile(evt) {
            //Retrieve the first (and only!) File from the FileList object
            var myFile = evt.target.files[0];
            var reader = new FileReader();
            reader.readAsText(myFile);
            var myString = reader.toString();
            alert(myString); // print  - "[object FileReader]"
        }

I try to get all the file content into String , for example if the file content is

helloWorld1
helloWorld2

I will get alert of that's content .


回答1:


That's not how you get the result of a FileReader. Modify your code to this:

function readSingleFile(evt) {
        //Retrieve the first (and only!) File from the FileList object
        var myFile = evt.target.files[0];
        var reader = new FileReader();
        reader.readAsText(myFile);
        reader.onload=function(){alert(reader.result)}
    }


来源:https://stackoverflow.com/questions/21962032/filereader-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!