HTLF

一步一个脚印,走出高度...

导航

前端获取dom结构,转成多文件导出压缩包下载

需要插件

  1. jszip
  2. file-saver

引入

<script src="~/lib/jszip/jszip.js?t=@jsAndCssVersion"></script>
<script src="~/lib/jszip/FileSaver.min.js?t=@jsAndCssVersion"></script>

使用

var zip = new JSZip();
// 获取iframe 节点
var iframe = document.getElementById("layui-layer-iframe1");
// 获取 iframe 内部的文档
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

$.each(iframeDoc.querySelectorAll(".item"), function (i, v) {
    // 转成blod 
    const blod = new Blob([$(v).html()], {
        type: 'application/file'
    })
    // 添加到 zip中
    zip.file( $(v).attr("data-name")+ ".docx", blod);
})

zip.generateAsync({ type: "blob" })
    .then(function (content) {
        // 下载
        saveAs(content, $(".layui-layer-title").html()+".zip");
    });

posted on 2025-01-08 18:09  HTLF  阅读(23)  评论(0)    收藏  举报