React 下载树形结构文件目录压缩包

onClickDownloadBtn(row) {
        // TODO: delete Demo Data
        const files = [
            {
                id: 1,
                name: '1file',
                isFile: false,
                children: [
                    {
                        id: 11,
                        name: '1-1file',
                        isFile: false,
                        children: [
                            {
                                id: 111,
                                name: '1-1-1file',
                                isFile: true,
                                url: 'https://im-1305783521.cos.ap-guangzhou.myqcloud.com/im/file/lilinjian/1641631041478/dd.txt'
                            }
                        ]
                    }, {
                        id: 12,
                        name: '1-2file',
                        isFile: true,
                        url: 'https://im-1305783521.cos.ap-guangzhou.myqcloud.com/im/file/C13012345678/1641805597561/巴达兽.png'
                    }
                ]
            }, {
                id: 2,
                name: '2file',
                isFile: true,
                url: 'https://im-1305783521.cos.ap-guangzhou.myqcloud.com/im/file/C13012345678/1645261335302/测试文档.doc'
            }, {
                id: 3,
                name: '3folder',
                isFile: false,
            }
        ]
        const zip = new JSZip()
        let result = []
        files.forEach((file) => {
            this.addFileOrFolder(file, '', zip, result);
         })
        Promise.all(result).then(() => {
            zip.generateAsync({ type: "blob" }).then((res) => {
                saveAs(res, row.title + ".zip")
            })
        })
    }

    addFileOrFolder(file, prefix, zip, result) {
        let fileName = file.name;
        if (prefix) {
            fileName = prefix + '/' + file.name;
        }
        if (!file.isFile) {
            if (file.children && file.children.length) {
                file.children.forEach((subFile) => {
                    this.addFileOrFolder(subFile, fileName, zip, result);
                });
            } else {
                zip.folder(fileName);
            }
        } else {
            let promise = this.getFileBlob(file.url).then((res) => {
                let format = file.url.substring(file.url.lastIndexOf("."), file.url.length)
                zip.file(fileName + format, res, { binary: true })
            })
            result.push(promise);
        }
    }

    getFileBlob(url) {
        return new Promise((resolve, reject) => {
            let request = new XMLHttpRequest()
            request.open("GET", url, true)
            request.responseType = "blob"
            request.onload = (res) => {
                if (res.target.status === 200) {
                    resolve(res.target.response)
                } else {
                    reject(res)
                }
            }
            request.send()
        })
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值