File()The File() constructor creates a new File object instance. grammarvar myFile = new File(bits, name[, options]); parameter bits An Array containing name options optional Options object containing optional properties for the file. The available options are:
Examplevar file = new File(["my name"], "infoTxt", { type: "text/plain", }); Blob()A Blob object represents an immutable, raw file-like object. Its data can be read in text or binary format, and can also be converted into a ReadableStream for data manipulation. Blob does not 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. The API of the Blob object is also listed in the File interface. To construct a Blob from other non-blob objects and data, use the Blob() constructor. To create a subset of a blob's data, use the slice() method. To get a Blob object corresponding to a file on the user's file system, see the File documentation. grammarvar aBlob = new Blob( array, options ); Returns a newly created Blob object whose content consists of the concatenation of the arrays given in the parameters. parameter
property Blob.size Read-only The size, in bytes, of the data contained in the Blob object. Blob.type Read-only A string indicating the MIME type of the data contained in this Blob object. If the type is unknown, the value is an empty string. methodBlob.slice([start[, end[, contentType]]]) Returns a new Blob object containing the data in the specified range of the source Blob object. Blob.stream() Returns a Blob.text() Returns a promise that resolves to Blob.arrayBuffer() Returns a promise that contains Exampleconst aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // An array containing DOMStrings const oMyBlob = new Blob(aFileParts, {type : 'text/html'}); // Get the blob oMyBlob.size // 32 oMyBlob.type // 'text/html' The Blob() constructor allows you to create a Blob object from other objects. For example, to construct a blob from a string: var debug = {hello: "world"}; var blob = new Blob([JSON.stringify(debug, null, 2)], {type : 'application/json'}); Use Blob to create a URL pointing to a typed array const imgBlob = fetchedImgData(); // Image resource returned through the interface, set the returned responseType to blob const blob = new Blob([imgBlob], {type: 'image/png' }); // Pass in a suitable MIME type const url = URL.createObjectURL(blob); // Will generate a URL string like blob:d3958f5c-0777-0845-9dcf-2cb28783acaf // You can use it like a normal URL, such as on img.src. Extracting data from a Blob One way to read the contents from a Blob is to use a FileReader. The following code reads the contents of a Blob as a typed array: const reader = new FileReader(); reader.readAsArrayBuffer(blob); reader.addEventListener("load ", function(readRes) { // readRes.target.result is converted to arrayBuffer's blob }); Another way to read the contents of a Blob is to use the Response object. The following code reads the contents of a Blob as text: var text = await new Response(blob).text(); The Blob can be read as a string or data URL by using other methods of FileReader. SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Gradient slide effect implemented by CSS3
>>: MySQL joint index effective conditions and index invalid conditions
Use JavaScript to implement a web page clock. The...
This article records the complete uninstallation ...
As shown below: The test command determines wheth...
Table of contents 1. v-for: traverse array conten...
About semantics Semantics is the study of the rel...
Preface In some cases, we only know the intranet ...
Problem description: After the front-end deletes ...
1. Overview Group by means to group data accordin...
Friends always ask me how to hide Linux processes...
The so-called cascading replication is that the m...
<br />I have compiled some domestic design w...
as follows: -m, --memory Memory limit, the format...
Table of contents Preface Development Environment...
Table of contents Writing Background Project Desc...
Be sure to remember to back up your data, it is p...