Thoroughly understand the blob object, realize file download, and file fragmentation technology

Blob object

The instance of Blob blob, blob is a binary large object, which is the encapsulation type of JavaScript for unmodifiable binary data. Arrays containing strings, ArrayBuffers, ArrayBufferViews, and even other blobs can be used to create blobs. The Blob constructor can accept an options parameter in which to specify the MIME type.

Create a Blob object

let blob = new Blob()

We can see
insert image description here
size => size of binary file
type => MIME type
Blob constructor

Blob(blobParts[, options])

method

You can see that there are some methods on the prototype
insert image description here
text => read the value of the blob
slice => can be used to further divide the data

Object URLs and Blobs

Object URL refers to the URL of data stored in File or Blob. The advantage of the URL of the object is that you can use the file without reading the file content into JavaScript.
To create an object URL, use the window.URL.createObjectURL() method to pass in a File or Blob object. The value returned by this function is a string pointing to an address in memory. Because this value is a URL, it can be used directly in the DOM.

Implement download files

Example: res is a binary data blob

  	const href = URL.createObjectURL(res);
	const a = document.createElement("a");
	a.download = "物料使用记录表.xlsx";
	a.href = href;
	a.click();

file fragmentation

I'll add it later, I'm busy with work recently, haha

Replenish

MIME type

A media type (commonly known as Multipurpose Internet Mail Extensions or MIME for short)
is a standard used to represent the format and nature of a document/file/byte stream.
Browsers are using MIME to determine how to handle the
common type of URL

text/plain
text/html
image/jpeg
image/png
audio/mpeg
audio/ogg
audio/*
video/mp4
application/*
application/json
application/javascript
application/ecmascript
application/octet-stream

Guess you like

Origin blog.csdn.net/qq_45859670/article/details/127320186