读取base64图片File
file2Base64Image(file, cb) { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function (e) { cb && cb(e.target.result);//即为base64结果 }; },
读取text、JSON文件File
readText(file, { onloadend } = {}) { const reader = new FileReader(); reader.onloadend = function (e) { onloadend && onloadend(reader.result, e); }; reader.readAsText(file); },
读取分段Blob文件File(ArrayBuffer)
readArrayBuffer(file, { onloadstart, onprogress, onload } = {}) { const reader = new FileReader(); // 开始读取 reader.onloadstart = function (e) { onloadstart && onloadstart(e); }; // 正在加载 reader.onprogress = function (p) { onprogress && (parseFloat(p.loaded / file.size).toFixed(2)); }; // 加载完成 reader.onload = function (e) { if (reader.error) { return; } let buffer = new Uint8Array(reader.result); onload && onload(reader.result, buffer, e); }; reader.readAsArrayBuffer(file); },