一个Blob对象就是一个包含有只读原始数据的类文件对象,被浏览器“视同文件”。File 接口基于Blob,继承了Blob的功能,并且扩展支持了用户计算机上的本地文件。
Blob对象仅仅是一个可以设置MIME类型的,二进制数据的容器,真正的业务功能还需要通过FileReader、URL、Canvas等对象实现
获取Blob
通过<input type="file">即可获取本地文件的Blob对象
imageSelector.addEventListener('change', (event) => {
const file = event.target.files[0];
console.log(file instanceof Blob);//true
});
创建Blob
1.通过构造函数
var blob = new Blob(dataArray:Array<any>, opt:{type:string});
- dataArray:数组,包含了要添加到Blob对象中的数据,数据可以是任意多个ArrayBuffer,ArrayBufferView, Blob,或者 DOMString对象。
- opt:对象,用于设置Blob对象的属性(如:MIME类型)
2.通过Blob.slice()
Blob.slice(start:number, end:number, contentType:string)
- 此方法返回一个新的Blob对象,包含了原Blob对象中指定范围内的数据
3.通过canvas.toBlob()
var canvas = document.getElementById("canvas");
canvas.toBlob(function(blob){
console.log(blob);
});
读取Blod
从Blob中读取二进制内容的唯一方法是使用 FileReader。
FileReader 对象允许异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据。
var reader = new FileReader();
reader.addEventListener("loadend", function(e) {
// reader.result(或e.target.result) 包含转化为类型数组的blob
});
reader.readAsArrayBuffer(blob);
为Blob创建Url用于下载文件 / 显示图片
window.URL对象将Blob对象视为一个本地文件,并可以为其生成一个Url
- 结合a标签的download属性,可以实现点击url下载文件
createDownload("download.txt","download file");
function createDownload(fileName, content){
var blob = new Blob([content]);
var link = document.createElement("a");
link.innerHTML = fileName;
link.download = fileName;
link.href = URL.createObjectURL(blob);
document.getElementsByTagName("body")[0].appendChild(link);
}
- 结合img标签的src属性,可以实现图片预览
const imageSelector = document.getElementById('imageSelector');
const imagePreview = document.getElementById('imagePreview');
imageSelector.addEventListener('change', (event) => {
const file = event.target.files[0];
const url = URL.createObjectURL(file);
imagePreview.src = url;
});
使用Blob生成xls
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<style>
/* 此样式仅用于浏览器页面效果,Excel不会分离表格边框,不需要此样式 */
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<!-- 设置border="1"以显示表格框线 -->
<table border="1">
<!-- caption元素可以生成表标题,其单元格列跨度为表格的列数 -->
<caption>学生成绩表</caption>
<tr>
<!-- 可以使用rowspan和colspan来合并单元格 -->
<th rowspan="2">学号</th>
<th rowspan="2">姓名</th>
<th rowspan="2">性别</th>
<th rowspan="2">年龄</th>
<th colspan="2">成绩</th>
</tr>
<tr>
<th>语文</th>
<th>数学</th>
</tr>
<tr>
<td>1</td>
<td>张三</td>
<td>男</td>
<td>13</td>
<td>85</td>
<td>94</td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>女</td>
<td>12</td>
<td>96</td>
<td>84</td>
</tr>
</table>
<a>导出表格</a>
<script>
// 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码
var html = "<html><head><meta charset='utf-8' /></head><body>" + document.getElementsByTagName("table")[0].outerHTML + "</body></html>";
// 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象
var blob = new Blob([html], { type: "application/vnd.ms-excel" });
var a = document.getElementsByTagName("a")[0];
// 利用URL.createObjectURL()方法为a元素生成blob URL
a.href = URL.createObjectURL(blob);
// 设置文件名
a.download = "学生成绩表.xls";
</script>
</body>
</html>
查看chrome://blob-internals/,可以查看最近的Blob:

此外canvas也可以通过toDataURL的方式建立下载
imgData = canvas.toDataURL('png');
imgData = imgData.replace('image/png','image/octet-stream');
link.href = URL.createObjectURL(blob);