Giter Site home page Giter Site logo

blogs's People

Contributors

fredafei avatar

Watchers

 avatar  avatar

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,最容易出现的问题就是越界,可通过比较长度发现问题。

参考资源

Blob MDN
File MDN
ArrayBuffer MDN
binary javascript.info

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.