0
点赞
收藏
分享

微信扫一扫

vue 导出文件流

孟佳 2022-02-08 阅读 39

html:
下载文件名是获取的当前时间。

 <div
                  class="search-btn"
                  @click="exports"
                  style="width: 60px; cursor: pointer"
                  v-if="total > 0"
                >
                  <svg-icon icon-class="daochu" class="sousuo"></svg-icon>
                  导出
                </div>

js

  exports() {
      exports()
        .then((res) => {
          this.$message({
            message: "导出成功",
            type: "success",
          });
          this.loading = false;
          console.log(res, "res=======");
          const content = res;
          const blob = new Blob([content], { type: "application/x-xls" });
          var today = new Date();
          var month = today.getMonth() + 1;
          month = month < 10 ? "0" + month : month;
          var day =
            today.getDate() < 10 ? "0" + today.getDate() : today.getDate();
          var hours =
            today.getHours() < 10 ? "0" + today.getHours() : today.getHours();
          var mins =
            today.getMinutes() < 10
              ? "0" + today.getMinutes()
              : today.getMinutes();
          var secs =
            today.getSeconds() < 10
              ? "0" + today.getSeconds()
              : today.getSeconds();

          // var now1 = today.getFullYear() + "/" + month + "/" + day;
          var now1 =
            today.getFullYear() +
            "-" +
            month +
            "-" +
            day +
            " " +
            hours +
            "-" +
            mins +
            "-" +
            secs;
          console.log(now1, "nameDate00");
          const fileName = now1 + ".xls";
          if ("download" in document.createElement("a")) {
            // 非IE下载
            const elink = document.createElement("a");
            elink.download = fileName;
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
          } else {
            // IE10+下载
            navigator.msSaveBlob(blob, fileName);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
举报

相关推荐

0 条评论