前端下载流,及base64,导出excel

1.下载流
 downLoadFile = (option) => {
        let params = {
            originalFileName: option.file.originalFileName,
            relativePath: option.file.relativePath,
            newFileName: option.file.newFileName
        };  
        unCrudeHttp({
            method: 'post',
            url: `${window.API_HOST}/business/resourcefile/download`,
            responseType: "blob",
            data: params
        }).then(res => {
            let data = res.data;
            const blob = data;
            const fileName = `${option.file.originalFileName}`;
            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);
            document.body.removeChild(elink);
        });
    }


2.下载base64
 downLoadFile = (option) => {
      
        let params = {
            originalFileName: option.originalFilename,
            relativePath: option.sftpVO.directory,
            newFileName: option.sftpVO.fileName
        };  
        billingSystemApi.fileDownloadApi(params).then(res => {    
            let  content = res.data
            // 使用atob方法解码base64
            var raw = window.atob(content);
            // 创建一个存储解码后数据的数组
            var uInt8Array = new Uint8Array(raw.length);
            // blob只能接收二进制编码,需要讲base64转为二进制再塞进去
            for (var i = 0; i < raw.length; ++i) {
                uInt8Array[i] = raw.charCodeAt(i);
            }
            // 这里给了一个返回值,在别的方法掉用传入base64编码就可以得到转化后的blob
            const link = document.createElement('a')
            const blob = new Blob([uInt8Array], { type: 'application/vnd.ms-excel' })
            link.style.display = 'none'
            link.href = URL.createObjectURL(blob)
            //设置下载的Excel表名
            link.setAttribute('download', `${option.originalFilename}`)  //要有文件名后缀,如.xslx
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
            this.setState({loading: false})

        }).catch(err=>{
            this.setState({loading: false})
            message.error('导出失败!')
        })
    }

base64返回结果:
{
“code”:“2000”,
“message”:“成功”,
“data”:“UEsDBBQACAgIuhtjO4BNFtJTGLL2NqWvf3MLltLChvsKCR9/wfSdj+HSb1RLp6jgXXTgqJo2fnYG3g+PazuQBXB6HDiSANhb…S54bWxdDIueG1sUEsFBgAAAAAKAAoAhQIAAO0PAAAAAA==”,
“traceId”:“b0bc2153abcefba6”
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值