zoukankan      html  css  js  c++  java
  • 【angular5项目积累总结】文件下载

    download() {
    
    
    const token = localStorage.getItem('token');
    
    
    let headers: HttpHeaders = new HttpHeaders();
    
    
    headers = headers
    
    .set('Authorization', 'Bearer ' + token);
    
    const url = 'http://localhost:8764/api/v1/user/downLoadZipFile';
    
    
    this.http.get(url, {headers: headers, observe: 'response', responseType: 'blob'}).subscribe(response => {
    
    
    console.log(response);
    
    
    console.log(response.headers.keys());
    
    
    this.downloadFile(response);
    
    }, (error: HttpErrorResponse) => {
    
    
    console.log(error.error);
    
    });
    
    }
    
    
    
    downloadFile(data: HttpResponse<Blob>) {
    
    
    const file = new Blob([data.body], {type: 'application/zip'});
    
    
    const a = document.createElement('a');
    
    
    a.id = 'tempId';
    
    document.body.appendChild(a);
    
    
    a.download = 'haha.zip';
    
    
    a.href = URL.createObjectURL(file);
    
    
    a.click();
    
    
    const tempA = document.getElementById('tempId');
    
    
    if (tempA) {
    
    
    tempA.parentNode.removeChild(tempA);
    
    }
    
     
    
    }
    
    }
    var blob = new Blob([res], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});  
    var objectUrl = URL.createObjectURL(blob);  
    var a = document.createElement('a');
    document.body.appendChild(a);
    a.setAttribute('style', 'display:none');
    a.setAttribute('href', objectUrl);
    a.setAttribute('download', fileName);
    a.click();
    URL.revokeObjectURL(objectUrl); 

    通用下载代码JS:

     downloadFile(filePath: any) {
        this.meetingService.downloadFile(filePath, rtv => {
          if (rtv) {
            let _blob = new Blob([rtv]);
            let _filename = filePath.substring(filePath.lastIndexOf('_') + 1);
            if (window.navigator && window.navigator.msSaveOrOpenBlob) {
              window.navigator.msSaveOrOpenBlob(_blob, _filename);
            } else {
              let _link = document.createElement('a');
              let _url = window.URL.createObjectURL(_blob);
              document.body.appendChild(_link);
              _link.setAttribute('style', 'display:none');
              _link.href = _url;
              _link.download = _filename;
              _link.click();
              window.URL.revokeObjectURL(_url);
              _link.remove();
            }
          } else {
            alert('下载失败,请稍后重试!');
          }
        });
      }

    参考文章:

    https://www.cnblogs.com/liugang-vip/p/7016733.html

  • 相关阅读:
    NodeJS实例系列~环境搭建,Hello world归来!
    Node.js教程系列~目录
    poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
    利用手工编码的方式对srtus2进行输入验证
    介绍linux下Source Insight强大代码编辑器sublime_text_3
    【机器学习】支持向量机[续1]
    boost库在工作(33)网络服务端之三
    HNCU1099:堆积木
    HNCU1100:彩票
    Lua获取网络时间
  • 原文地址:https://www.cnblogs.com/sybboy/p/9214094.html
Copyright © 2011-2022 走看看