Blob文件流下载

  
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);
  }

你可能感兴趣的:(javascript)