javascript FileReader 转字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21962032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:05:53  来源:igfitidea点击:

FileReader to String

javascriptfilereader

提问by URL87

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

我尝试将所有文​​件内容放入String,例如,如果文件内容是

helloWorld1
helloWorld2

I will get alertof that's content .

我会明白alert的。

回答by markasoftware

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

这不是您获得 a 的结果的方式FileReader。将您的代码修改为:

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)}
    }