Using Javascript FileReader with huge files

2019-01-18 06:43发布

I have a problem using the Javascript FileRead trying to read huge files.

For example, I have a text file of 200mb and everytime I read this file the code stops working.

Its possible to read the text file, but for example ONLY the first 10 lines or stop reading after 10mb?

This is my code:

var file = form.getEl().down('input[type=file]').dom.files[0];
var reader = new FileReader();

reader.onload = (function(theFile) {
                return function(e) {
                    data = e.target.result;
                    form.displayedData=data;
                };
            })(file);

reader.readAsText(file);

The e.target.result always has the whole data of the file.

What can I do here?

Thx

1条回答
不美不萌又怎样
2楼-- · 2019-01-18 07:22

This will only read the first 10 mb:

var file = form.getEl().down('input[type=file]').dom.files[0];
var reader = new FileReader();

reader.onload = function(e) {
    var data = e.target.result;
    form.displayedData = data;
};

reader.readAsText(file.slice(0, 10 * 1024 * 1024));
查看更多
登录 后发表回答