一、封装函数

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

    二、后台返回文件流
    传参、调接口,下载文件

    1. let params = {
    2. fileName: val.fileName,
    3. filePath: val.filePath
    4. };
    5. const res = await DownLoad(params);
    6. const { data, headers } = res;
    7. if (!headers[“content-disposition”]) {
    8. this.$message.error(“导出数据为空”);
    9. return false;
    10. }
    11. const fileNames = headers[“content-disposition”].split(
    12. fileName=”
    13. );
    14. console.log(fileNames);
    15. const fileName = decodeURI(fileNames[fileNames.length - 1]);
    16. this.downloadText(data, fileName);