ODe*_*lta 34 javascript html5 filereader
到目前为止,我的文件阅读器api代码一直运行良好,直到有一天我从我的一个客户端获得了一个280MB的txt文件.页面只是在Chrome中直接崩溃,在Firefox中没有任何反应.
// create new reader object
var fileReader = new FileReader();
// read the file as text
fileReader.readAsText( $files[i] );
fileReader.onload = function(e)
{ // read all the information about the file
// do sanity checks here etc...
$timeout( function()
{
// var fileContent = e.target.result;
// get the first line
var firstLine = e.target.result.slice(0, e.target.result.indexOf("\n") ); }}
Run Code Online (Sandbox Code Playgroud)
我上面要做的是获取第一个换行符,以便我可以获得文件的列长度.我不应该把它看作文字吗?如何在不破坏大文件页面的情况下获取文件的列长度?
Rob*_*b W 63
您的应用程序无法处理大文件,因为您在处理之前将完整文件读入内存.这种低效率可以通过流式传输文件(读取小块的块)来解决,因此您只需要将一部分文件保存在内存中.
甲File对象也是一个实例Blob,它提供了.slice创建文件的较小视图的方法.
这是一个假设输入为ASCII的示例(演示:http://jsfiddle.net/mw99v8d4/).
function findColumnLength(file, callback) {
// 1 KB at a time, because we expect that the column will probably small.
var CHUNK_SIZE = 1024;
var offset = 0;
var fr = new FileReader();
fr.onload = function() {
var view = new Uint8Array(fr.result);
for (var i = 0; i < view.length; ++i) {
if (view[i] === 10 || view[i] === 13) {
// \n = 10 and \r = 13
// column length = offset + position of \r or \n
callback(offset + i);
return;
}
}
// \r or \n not found, continue seeking.
offset += CHUNK_SIZE;
seek();
};
fr.onerror = function() {
// Cannot read file... Do something, e.g. assume column size = 0.
callback(0);
};
seek();
function seek() {
if (offset >= file.size) {
// No \r or \n found. The column size is equal to the full
// file size
callback(file.size);
return;
}
var slice = file.slice(offset, offset + CHUNK_SIZE);
fr.readAsArrayBuffer(slice);
}
}
Run Code Online (Sandbox Code Playgroud)
上一个片段计算换行前的字节数.计算由多字节字符组成的文本中的字符数稍微困难一些,因为您必须考虑到块中的最后一个字节可能是多字节字符的一部分的可能性.
有一个很棒的库叫做Papa Parse,它以一种优雅的方式做到了这一点!它真的可以处理大文件,你也可以使用 web worker。
只需尝试他们提供的演示:https : //www.papaparse.com/demo
| 归档时间: |
|
| 查看次数: |
21816 次 |
| 最近记录: |