FileReader到String [英] FileReader to String

查看:191
本文介绍了FileReader到String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码

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

我将获得 alert 那是内容。

推荐答案

这不是你得到 FileReader 的结果。将您的代码修改为:

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

这篇关于FileReader到String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆