当前位置: 代码迷 >> 综合 >> 前端文件下载
  详细解决方案

前端文件下载

热度:83   发布时间:2024-01-25 00:03:22.0
  1. <a> 标签 下载 


    HTML原理 

    //创建URL 利用a标签属性下载
    <a href="url" download="<fileName>">导出/下载</a>

    JS实现方法

          let blobUrl = window.URL.createObjectURL(result);const a = document.createElement('a')a.style.display = 'none';a.download = '<線體信息>';a.href = blobUrl;a.click();

    数据导出 

    exportFile() {fetch(url, {method: 'POST',body: JSON.stringify(arg),headers: {'Content-Type': 'application/json; charset=utf-8'}}).then(res => {//二进制文件转换  .json()return res.blob();}).then(result => {let blobUrl = window.URL.createObjectURL(result)const a = document.createElement('a')a.style.display = 'none';a.download = '<fileName>';a.href = blobUrl;a.click();});}//GETlet url = `${this.intSrv.daoChuData}?flag=${this.parmas.flag}&lineName=${this.parmas.lineName}`const a = document.createElement('a')a.style.display = 'none';a.setAttribute('target', '_blank');a.href = url;a.click();

     

 

  相关解决方案