栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

大文件上的filereader API

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

大文件上的filereader API

您的应用程序无法处理大文件,因为您在处理之前将整个文件读入内存。这种低效率可以通过流式传输文件(读取小块)来解决,因此您只需要在内存中保留文件的一部分即可。

File
对象也是一个实例
Blob
,它提供了
.slice
创建文件的较小视图的方法。

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);    }}

上一个代码段计算换行之前的字节数。计算由多字节字符组成的文本中的字符数稍微困难一点,因为您必须考虑到块中的最后一个字节可能是多字节字符的一部分的可能性。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614910.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号