axios通过get方式下载Excel

 vue+element环境下的写法,其它框架也基本一样

主要是将后台返回的数据处理之后下载为.xlsx的文件,再模拟点击下载

      axios({
        method: "get",
        url: "url****",
        responseType: "blob",    // 重点
        headers: {***},
        params: params,
      })
        .then((res) => {
          let url = window.URL.createObjectURL(new Blob([res.data]));
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", this.$route.meta.title + ".xlsx");

          document.body.appendChild(link);
          link.click();
        })
        .catch(() => {
          this.$message.error("网络错误!");
        });

猜你喜欢

转载自blog.csdn.net/joyvonlee/article/details/107998339
今日推荐