nonsense
React downloads bloB files in the form of file streams from the back end. Blob files run like blob files. Blob files run like BLOB files in the back end Next time I wanted to fight Valentino, tear up Balenciaga, uncover Victoria’s Secret and explore the Norwegian forest
The Stream concept
Imagine that you write a file, you can save it on a hard drive, you can save it on a USB drive, and that’s the transfer of data. The transmission of data, that is, the flow of data, and since there is a flow, there is an out and an in, like most rivers, a trickle at one point and a final flow into the sea. In many cases, a Stream is referred to as a Byte Steram, which is a long string of bytes. In addition to the Byte Stream, we may have a video Stream, an audio Stream, a data Stream, and so on……
Streams can be divided into byte streams and character streams according to the unit of data processed. Byte streams are processed in bytes and are usually used to process binary files such as music, image files, etc. The processing unit of character stream is character. Because Java uses Unicode encoding, Java character stream is dealing with Unicode characters. Therefore, character stream has advantages in operation of Chinese characters and internationalization. The concept of flow is a bit abstract, so we won’t go into it too much. You can learn by yourself if you are interested
MIME concept
A media type (often called a Multipurpose Internet Mail Extensions or MIME type) is a standard used to represent the nature and format of a document, file, or byte stream. It is defined and standardized in IETF RFC 6838. The Internet Assigned Numbers Authority (IANA) is the official body responsible for keeping track of all official MIME types, and you can find the full list up to date on the Media Types page.
MIME, to give it its full name, Multipurpose Internet Mail Extensions, was originally customized to extend plain text E-mail to support a variety of message formats. It has since been applied to a variety of protocols, including HTTP, which we often use. The common form of MIME is a main type followed by a subtype separated by a slash. Such as Text/HTML, Application /javascript, image/ PNG, etc.
We can't teach Robin Li how to do Baidu, we can't teach Zhang Xiaolong how to do wechat, we can't teach Cook how to do iPhoneCopy the code
Blob
Concept – What is a Blob
A Blob (Binary Large Object) Object represents an immutable, raw data-like file Object. Its data can be read in text or binary format, or converted to ReadableStream for data manipulation. Blobs don’t necessarily represent data in JavaScript’s native format. The File interface is based on Blob, inheriting the functionality of Blob and extending it to support files on the user’s system.
From the official definition of MDN, we can probably know what the Blob is used for, it is a device to read or manipulate binary data, so that we can download the file stream from the back end
usage
The constructor
var Blob = new Blob( array, options )
Copy the code
-
An array is an array of objects such as an ArrayBuffer, an ArrayBufferView, a Blob, a DOMString, or a mixture of similar objects, It will be put into the Blob. DOMStrings will be encoded to UTF-8.
-
Options are optional and may specify the following two properties:
-
Type, which defaults to “”, represents the MIME type of the array content to be put into the BLOB.
-
- Hamed, whose default value is “transparent”, specifies how the string containing the line end character n is written. It is one of two values:
-
- “Native”, which means that the line terminator is changed to a newline suitable for the host operating system file system, or “transparent”, which means that the end character saved in the BLOb is left unchanged.
🌰 :
var say = {hello: "world"}
var blob = new Blob([JSON.stringify(say, null.2)] and {type : 'application/json'})
Copy the code
The URl object
We want to download this file by creating a URL object specifying the download link for the file
Create a constructor
Creates a new URL representing the specified File object or Blob object.
objectURL = window.URL.createObjectURL(blob)
Copy the code
Release object
This method doesn’t take care of things for you like JavaScript’s garbage collection mechanism, which creates a new URL object every time you call createObjectURL, even if you’ve already created it with the same object as an argument. When these URL objects are no longer needed, each object must be released by calling the url.revokeObjecturl method. Browsers automatically release documents when they exit, but for best performance and memory usage, you should actively release them when it’s safe to do so.
window.URL.revokeObjectURL(objectURL)
Copy the code
Use the A tag to download
Generate an A tag
const link = document.createElement('a')
Copy the code
Specify download link
link.href = window.URL.createObjectURL(blob)
Copy the code
Setting the file name
link.download = fileName
Copy the code
Triggering event
link.click()
Copy the code
download
const foo = {
hello: "world"
};
const blob = new Blob([JSON.stringify(foo)], {
type: 'application/vnd.ms-excel; charset=utf-8'
});
const fileName = `The ${new Date().valueOf()}.xls`;
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
window.URL.revokeObjectURL(link.href);
Copy the code
Compatible with IE
In IE to use window. The navigator. MsSaveOrOpenBlob to handle a Blob object.
window.navigator.msSaveOrOpenBlob(blob, fileName);
Copy the code
The overall code
/*res is the requested file stream returned, and name is the passed file name */
export const downloadExcel = (res,name) = > {
const blob = new Blob([res],{type: 'application/vnd.ms-excel'})
const fileName = `${name}.xlsx`
if ('download' in document.createElement('a')) {
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
} else {
navigator.msSaveBlob(blob, fileName)
}
}
Copy the code
Blob: MIME types are common in JavaScript. Why do YOU need MIME types when you have file extensions? The front-end uses Blob objects to create and download specified files