当前位置:   article > 正文

js实现下载_js download

js download

一、封装函数

  downloadText(data, name) {
            const id = "__ETL_DOWNLOAD_BUTTON__";
            const blob = new Blob([data]);
            const url = URL.createObjectURL(blob);
            let a = document.getElementById("#" + id);
            if (!a) {
                a = document.createElement("a");
                a.id = id;
                document.body.append(a);
            }
            a.href = url;
            a.download = name;
            a.click();
   },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

二、后台返回文件流
传参、调接口,下载文件
let params = {
fileName: val.fileName,
filePath: val.filePath
};
const res = await DownLoad(params);
const { data, headers } = res;
if (!headers[“content-disposition”]) {
this.$message.error(“导出数据为空”);
return false;
}
const fileNames = headers[“content-disposition”].split(
“fileName=”
);
console.log(fileNames);
const fileName = decodeURI(fileNames[fileNames.length - 1]);
this.downloadText(data, fileName);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/63642
推荐阅读
相关标签
  

闽ICP备14008679号