Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually trigger file reader onerror event?

Tags:

javascript

I'm testing all the scenarios on reading files using new FileReader(); When the read is done, onload() should be triggered. I have no idea how to trigger onerror() callback Any idea?

        var promise = Promise.create();
        var fileReader = new FileReader();
        fileReader.onload = function () {
        };
        fileReader.onerror = function(event) {
            // I want to stay here ..........
            // Who can tell me how to trigger this onerror callback?
            debugger
        };
        fileReader.readAsText(inputFile);
        return promise;

like image 765
newBike Avatar asked Jul 01 '26 06:07

newBike


1 Answers

Try simply calling abort:

var promise = Promise.create();
var fileReader = new FileReader();
fileReader.onload = function () {
  fileReader.abort()
};
fileReader.onerror = function(event) {
  // I want to stay here ..........
  // Who can tell me how to trigger this onerror callback?
  debugger
};
fileReader.readAsText(inputFile);
return promise;

If that doesn't work you can try a permissions error with something like:

touch testfile
chmod 000 testfile

And open that file from your reader.

like image 72
Marcos Luis Delgado Avatar answered Jul 02 '26 20:07

Marcos Luis Delgado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!