-1

Good Day,

I have this code:

function loadFile() {
 var fileToLoad = document.getElementById("loadMe").files[0];
 var fileStart = 0;
 var fileEnd = fileToLoad.size - 1;
 var fileReader = new FileReader();
 fileReader.onload = function(fileLoadedEvent) {
    var textFromFileLoaded = fileLoadedEvent.target.result;
    var string = textFromFileLoaded.split('&');
    pleaseDoSomethingWithTheContentOfTheFile(textFromFileLoaded);
 };
fileReader.readAsText(fileToLoad, "UTF-8");
}

I have an error :

Uncaught TypeError: Cannot read property 'size' of undefined at loadFile

According to this post there is no index [0]... But when I remove that, I still have the same error. Is this something coming from this code, or does it look good ? Thanks in advance! :)

EricF
  • 181
  • 1
  • 3
  • 19

1 Answers1

1

double check whether the file is selected before calling loadFile function.

also use below code checking the size

if (!fileToLoad) {
 alert('Pleae select file');
 return;
}
Immanuel
  • 167
  • 1
  • 8
  • Thank You, but I have an error `Uncaught TypeError: Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.` ... I'm not sure it's a value added to the code. – EricF Jul 18 '17 at 18:31
  • what is the size of the file?, instead of using onload eventm try onloadend, this link [link](https://stackoverflow.com/questions/33923985/parameter-is-not-of-type-blob) might help.. – Immanuel Jul 18 '17 at 18:52