Blob document processing

Blob object overview

Blob It represents an immutable objects, the object class of the original data file. Blob Not necessarily represent the JavaScriptdata in native format. File Interface is based on Blobinherited  Blob functionality and extend it to support files on the user's system.

grammar

const aBlob = new Blob( array, options ); 

Parameter Description

  • a is the Array array, or a mixture of other similar objects made of ArrayBuffer, ArrayBufferView, Blob, DOMString and other objects, which will be kept on Blob. DOMStrings is encoded as UTF-8.
  • options is an optional BlobPropertyBag dictionary, it might specify the following two properties:
    • type, default value is "", which represents the MIME type of the contents of the array will be put to the blob of.
    • endings, default is "transparent", for specifying a line terminator comprising \ n how the string is written. It is one of two values: "native", on behalf of line endings will be changed to fit the host operating system, file system, line breaks, or "transparent", on behalf of the blob will remain stored in the same endings

Examples

const debug = {hello: "world"};
const blob = new Blob([JSON.stringify(debug, null, 2)],{type : 'application/json'}); 

URL.createObjectURL () and URL.revokeObjectURL () Introduction

URL.createObjectURL() Static method creates a DOMString, which contains a URL object parameters given representation. The URL of the life cycle and create its window document binding. The new URL File object represents an object or Blob object specified. This method creates the equivalent of an incoming object referenced memory address

createObjectURL grammar

objectURL = URL.createObjectURL(object);

Parameter Description

  • object is used to create a URL File object, Blob object or MediaSource object.

return value

  • A possible reference to the specified objectDOMString

URL.revokeObjectURL() Static method used to release a previously existing by calling the  URL.createObjectURL() created  URL object. When you are finished using an  URL after object should call this method to let the browser know not to retain a reference to the file in memory.

You can  sourceopen call any time after it has been processed  revokeObjectURL(). This is because the  createObjectURL() only means to a media element  src associated attributes to a  MediaSource subject up. Call revokeObjectURL() to make this a potential target back to the original place, allowing the platform at the right time garbage collection .

revokeObjectURL grammar

window.URL.revokeObjectURL(objectURL);

Parameter Description

  • objectURL is a DOMString, represented by calling the  URL.createObjectURL() URL object produced by the method.

Memory Management

In each call createObjectURL() when the method will create a new URL object, even if you've been with the same objects created as a parameter. When these URL object is no longer needed, each object must call  URL.revokeObjectURL() to release method. The browser will automatically release them when the document exit, but for best performance and memory usage, you should take the initiative to relieve them in a safe opportunity.

Example: Excal download

axios({
                url: '',
                method: "post",
                responseType: 'arraybuffer',
            }).then((res) => {
                let blob = new Blob([res.data], {type: 'application/octet-stream'})
                let url = URL.createObjectURL(blob)
                let a = document.createElement('a')
                document.body.appendChild(a)
                a.href = url
                a.download = 'result.xls'
                a.target = '_blank'
                a.click();
                a.remove()
            })

  

 

 

  this. $http({
                 url:  this. $http. adornUrl( 'insurance/downloadAttachModel'),
                 method:  "post",
                 responseType:  'arraybuffer',
            }). then(( res=> {
                 let  blob =  new  Blob([ res. data], { type:  'application/octet-stream'})
                 let  url =  URL. createObjectURL( blob)
                 let  a =  document. createElement( 'a')
                 document. body. appendChild( a)
                 a. href =  url
                 a. download =  'result.xls'
                 a. target =  '_blank'
                 a. click();
                 a. remove()
            })

Guess you like

Origin www.cnblogs.com/gitwusong/p/12106092.html