Problems with HTML5 FileReader

I am trying to run the FileReader function in my project, but it only works in the pas.Mon project, so I am listing all the files in the directory. Everything is working. But when I select a file in the list (my files are local) I get this error, I don't understand:

Uncaught TypeError: Failed to execute 'readAsText' on 'FileReader': argument is not a block.

Here is my code:

GetFileName function (fs) {
     var target = event.target || event.srcElement;

     var childs = target.parentNode.childNodes;
     {for (i = 0; i ++; i <childs.length)
         if (target == childs [i]) break;
     }

     console.log (i); // Index li
     console.log (event.target.innerHTML); // li value

     filename = "~ / Downloads / snippets" + event.target.innerHTML;

var fr = new FileReader ();
fr.onload = function (e) {
     console.log (e.target.result);
};
fr.readAsText (filename);
}

      

Does anyone have any idea what isn't working?

Thank you in advance for your help!

+3


source to share


1 answer


I think the problem is that your variable is filename

set to:

filename = "~ / Downloads / snippets" + event.target.innerHTML;

      

... which is a string, not an object expected by a function readAsText

on FileReader

.

Try something like this:



var fileInputElement = document.getElementById("fileInputElement");
fr.readAsText(fileInputElement.files[0]);

      

Where "fileInputElement" is the id of the HTML <input>

type element file

.

This may not match exactly what you are doing, but it is not easy to tell without seeing more of your JavaScript and HTML.

+7


source







All Articles