Blob文件流下载

2021-03-03  本文已影响0人  Bonne_nuit
  
downLoad(msg) {
    const url1 = `url`;
    this.getBlob(`${url1}`).subscribe(result => {
      this.downFile(result, 'name.zip');
    })
  }


**//post方法**
requestBlob(url: any, data?: any): Observable<any> {
    return this.http.request("post", url, { body: data, observe: 'response', responseType: 'blob' });
  }


**//get方法**
  getBlob(url: any, data?: any): Observable<any> {
    return this.http.request("get", url, { body: data, observe: 'response', responseType: 'blob' });
  }


  **// Blob文件转换下载**
  downFile(result, fileName, fileType?) {
    const data = result.body;
    const blob = new Blob([data], { type: fileType || data.type });
    const objectUrl = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.setAttribute('style', 'display:none');
    a.setAttribute('href', objectUrl);
    a.setAttribute('download', fileName);
    a.click();
    URL.revokeObjectURL(objectUrl);
  }
上一篇 下一篇

猜你喜欢

热点阅读