fetch request data stream and downloads

And the front end of the acquisition stream data download.

1. The rear end of the setting response type

. 1 the response.setContentType ( "file application / OCTET-Stream"); // set the response type

2. By fetch distal acquisition stream

() Gets bolb by response.blob

. 1 app.fetch ( '/ xD / Editor / Total / Workload', function (Response) {
 2              response.blob (). The then ((BLOB) => {
 . 3                  saveBlobAs (BLOB, 'result.xls' )
 . 4              })
 . 5          }, function (Result) {
 . 6              console.info ( "data acquisition failure" + result.message);
 . 7          })
 . 8  
. 9  // method of preserving the bolb 
10  function saveBlobAs (BLOB, filename) {
 . 11          IF (window.navigator .msSaveOrOpenBlob) {
 12 is             navigator.msSaveBlob(blob, filename)
13         } else {
14             const anchor = document.createElement('a')
15             const body = document.querySelector('body')
16             anchor.href = window.URL.createObjectURL(blob)
17             anchor.download = filename
18 
19             anchor.style.display = 'none'
20             body.appendChild(anchor)
21 
22             anchor.click()
23             body.removeChild(anchor)
24 
25             window.URL.revokeObjectURL(anchor.href)
26         }
27     }

 

Guess you like

Origin www.cnblogs.com/jyughynj/p/12625983.html