下载blob
npm i blob --save接口
public exportDatabase2(databaseId: number): Observable<ArrayBuffer> { return this.http.get(`${this.dataExportUrl}?id=${databaseId}`, {responseType: 'arraybuffer'}); }调用接口下载(这里不知道为啥不走success方法,接口200)
this.schemaInfoServ.exportDatabase2(this.selectedDatabaseId) .subscribe((res) => {}, (error) => { this.nzMessageServ.remove(); this.isLoadingDownLoading = false; this.isShowExportDataModal = false; downLoadFile(error, `${this.selectedDatabaseName}-${moment(new Date()).format('YYYY-MM-DD')}` , 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); });downLoad方法
downLoadFile(data: ArrayBuffer, fileNmame: string, type: string): void { const blob = new Blob([data], { type }); const url = window.URL.createObjectURL(blob); const aLink= document.createElement('a'); aLink.download = `${fileNmame}.xls`; aLink.style.display = 'none'; aLink.href = url; // 触发点击 document.body.appendChild(aLink); aLink.click(); // 然后移除 document.body.removeChild(aLink); }