The Blob object represents a blob, which is a file-lượt thích object of immutable, raw data; they can be read as text or binary data, or converted into lớn a ReadableStream so its methods can be used for processing the data.
Bạn đang xem: Blob là gì
To construct a Blob from other non-blob objects & data, use the Blob() constructor. To create a blob that contains a subphối of another blob"s data, use the slice() method. To obtaimãng cầu Blob object for a tệp tin on the user"s file system, see the File documentation.
The APIs accepting Blob objects are also listed in the File documentation.
Blob() Returns a newly created Blob object which contains a concatenation of all of the data in the array passed into the constructor.
Blob.prototype.form size Read only The kích thước, in bytes, of the data contained in the Blob object. Blob.prototype.type Read only A string indicating the MIMEtype of the data contained in the Blob. If the type is unknown, this string is empty.
Blob.prototype.arrayBuffer() Returns a promise that resolves with an ArrayBuffer containing the entire contents of the Blob as binary data. Blob.prototype.slice() Returns a new Blob object containing the data in the specified range of bytes of the blob on which it"s called. Blob.prototype.stream() Returns a ReadableStream that can be used to read the contents of the Blob. Blob.prototype.text() Returns a promise that resolves with a USVString containing the entire contents of the Blob interpreted as UTF-8 text.
The Blob() constructor can create blobs from other objects. For example, to construct a blob from a JSON string:
const obj = hello: "world";const blob = new Blob(
p>This example creates a typed array containing the ASCII codes for the space character through the letter Z, then converts it to an object URL. A links to open that object URL is created. Click the links lớn see the decoded object URL.p>
function typedArrayToURL(typedArray, mimeType) return URL.createObjectURL(new Blob( One way to lớn read nội dung from a Blob is to use a FileReader. The following code reads the nội dung of a Blob as a typed array: const reader = new FileReader();reader.addEventListener("loadend", () => // reader.result contains the contents of blob as a typed array);reader.readAsArrayBuffer(blob);
Another way lớn read nội dung from a Blob is to lớn use a Response. The following code reads the content of a Blob as text:
One way to lớn read nội dung from a Blob is to use a FileReader. The following code reads the nội dung of a Blob as a typed array:
const reader = new FileReader();reader.addEventListener("loadend", () => // reader.result contains the contents of blob as a typed array);reader.readAsArrayBuffer(blob);