ArrayBuffer vs Blob and XHR2

◇◆丶佛笑我妖孽 提交于 2019-12-03 08:43:38

问题


XHR2 differences states

The ability to transfer ArrayBuffer, Blob, File and FormData objects.

  • What are the differences between ArrayBuffer and Blob ?
  • Why should I care about being able to send them over XHR2 ? (I can understand value of File and FormData)

回答1:


This is an effort to replace the old method which would take a "string" and cut sections of it out.

You would use an ArrayBuffer when you need a typed array because you intend to work with the data, and a blob when you just need the data of the file.

Blobs (according to spec anyway) have space for a MIME and easier to put into the HTML5 file API than other formats (it's more native to it).

The ArrayBuffer lets us work with typed arrays which is much faster than string manipulation to work with specific bytes and lets us define what type the array segments actually are. Since JavaScript is not strictly typed, it's hard to take a file that might be broken into an array of 32bit ints or perhaps 64bit floats (just imagine 8 bit ints-- that'd be a nightmare in terms of performance with string manipulation and bitwise calculations, especially with unicode).

As far as I can tell you can always move a blob to an array buffer or to a string representation, but this being native to XHR allows scripts to be faster which is the main advantage.

I'd use a blob for working with the file API, but I'd use the array for preforming computation on the data.



来源:https://stackoverflow.com/questions/7778115/arraybuffer-vs-blob-and-xhr2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!