最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to manually trigger file reader onerror event? - Stack Overflow

programmeradmin1浏览0评论

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;

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;

Share Improve this question edited Sep 19, 2019 at 21:54 newBike asked Sep 19, 2019 at 21:38 newBikenewBike 15k29 gold badges118 silver badges204 bronze badges 3
  • Maybe permissions? – Dennis Vash Commented Sep 19, 2019 at 21:39
  • You have to mock the FileReader. If you tell me which libraries are you using for testing maybe I can help you- – Marcos Luis Delgado Commented Sep 19, 2019 at 21:41
  • I'm coding on the REAL code. Just want have a quick test for now. Is it possible to trigger this without any testing lib or mock? – newBike Commented Sep 19, 2019 at 21:44
Add a ment  | 

3 Answers 3

Reset to default 5

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.

Maybe what you're trying to find out is how to use Async functions:

async function FileReader(){
    // your code for the file reading
  return;
};

then use await in your onload() function to wait for it to return:

async function onload(){
  await firstFunction();  // This waits for fileReader() function to finish...
  // do what you want to show when fileReading is done
};

Hope this helps!

You can run your code in debugger mode and add a breakpoint at line "fileReader.readAsText(inputFile)". When the debugger stops, you rename manually the file in your filesystem. FileReader then throws an error. The accepted answer doesn't work in my environment.

发布评论

评论列表(0)

  1. 暂无评论