blogs's People
blogs's Issues
javaScript 二进制数据
Blob
- 创建
const blob = new Blob(blobParts, options)
- 不可变,可使用slice读取blob 片段
Blob应用
- blob url,在
<a>、<img>
等的展示
const blobUrl = URL.createObjectURL(blob);
// do something
// ...
// 用完及时销毁
URL.revokeObjectURL(blobUrl)
- blob 转换为 base64
let reader = new FileReader();
reader.readAsDataURL(blob); // 将 Blob 转换为 base64 并调用 onload
reader.onload = function() {
console.log(reader.result); // data url
//...
};
注意:大的blob转base64后有性能和内存损耗。
ArrayBuffer
- 对固定长度的连续内存空间的引用
- 创建
const buffer = new ArrayBuffer(16); // 创建一个长度为 16 的 buffer
- 不是数组类型,不能使用array[index]的方式
- 使用view 访问
ArrayBuffer 应用
- 文件创建、上传、下载和图像处理
- ArrayBuffer 转blob
const buffer = new ArrayBuffer(16);
// 将 buffer 视为一个 32 位整数的序列, 它存储了 4 个整数
const view = new Uint32Array(buffer);
view[0] = 123456;
new Blob(view)
File 和 FileReader
- File 基于Blob扩展而来
- 创建
new File(fileParts, fileName, [options])
FileReader
是一个对象,读取File、Blob
数据- 除了使用
FileReader
,还可使用URL.createObjectURL(file)
File应用
- 上传文件
XMLHttpRequest/fetch
注意事项
- 文件,流,buffer,最容易出现的问题就是越界,可通过比较长度发现问题。
参考资源
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.