简体   繁体   English

无法通过FileReader读取文件

[英]Failed to read file through FileReader

Following is my snippet of code whcih reads file from client machine and it is working fine. 以下是我从客户端计算机读取文件的代码片段,它工作正常。

document.getElementById('files').onchange = function(e) { // Retrieve the file list from the input element //uploadFiles(e.target.files); document.getElementById('files')。onchange = function(e){//从输入元素中获取文件列表//uploadFiles(e.target.files);

Files =e.target.files;
   var reader = new FileReader();

            reader.onload = function (e)
            {
                alert("File loaded successfully");
 }reader.readAsText(e.target.files[0]); 

i am getting File loaded alert successfully. 我正在成功获取文件加载警报。 but when i copy this file object in json encoded format and then get this file object thorug json decode then it is not reading file. 但是,当我以json编码格式复制此文件对象,然后通过thorug json解码获取此文件对象时,它没有读取文件。

My code for encoding and decoding and then reading file is as follows: 我的编码和解码然后读取文件的代码如下:

indexArray="{'"LayerName":'"'+e.target.files[0]+'"}'

 var obj=JSON.parse(indexArray);

                        Files=obj.RGN;

                       if(obj.RGN)
                       {

                        var reader = new FileReader();
                        reader.onload = function (e)
                        {
                            alert("FIle loaded successfully");
                            var output=e.target.result;
                            console.log("output-----------------------------: "+output);

                        }

                        reader.readAsText(File);   

                       }
                       else
                       {
                           alert("Failed to load file");
                       }   

Now it is not working i am even not getting FAILED TO LOAD FILE. 现在无法正常工作,我什至无法加载文件。 so where i am doing wrong i have just encoded the file object and then decode and read that object? 所以在我做错的地方,我刚刚编码了文件对象,然后解码并读取了该对象?

Since obj.RGN is not a file object. 由于obj.RGN不是文件对象。 And Reader.readastext works only on file object (asynchronously). 而且Reader.readastext仅适用于文件对象(异步)。 You should equal Files to e.target.files then it will work properly. 您应该将文件等于e.target.files,然后它将正常工作。

ie

reader.onload = function (e) {

}

Files=e.target.files;
reader.readAsText(Files); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM