vue3 blob下载流文件

**问题前提:**需要把自己想要的内容转成txt文件里的内容并可以下载

解决过程:

首先,根据想要的文件后缀,确定blob的类型,

.doc对应application/msword,

.docx对应application/vnd.openxmlformats-officedocument.wordprocessingml.document,

.xls对应application/vnd.ms-excel,

.xlsx对应application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,

.txt对应text/plain,

其次,形成流文件时,一定要记得JSON.stringify

解决结果:

复制代码
const operRecordLog = ()=>{
    let operLog = JSON.stringify(list)//list是要放入txt文件中的内容
    const blob = new Blob([operLog], { type: 'text/plain' });
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = '日志.txt'; // 指定保存的文件名
    link.click()
    window.URL.revokeObjectURL(url);
}
相关推荐
kyriewen114 小时前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
Timer@5 小时前
LangChain 教程 04|Agent 详解:让 AI 学会“自己干活“
javascript·人工智能·langchain
阿珊和她的猫6 小时前
TypeScript中的never类型: 深入理解never类型的使用场景和特点
javascript·typescript·状态模式
skywalk81636 小时前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
RopenYuan8 小时前
FastAPI -API Router的应用
前端·网络·python
走粥8 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory0019 小时前
layui select重新渲染
前端·layui
weixin199701080169 小时前
《中国供应商商品详情页前端性能优化实战》
前端·性能优化
九皇叔叔10 小时前
003-SpringSecurity-Demo 统一响应类
java·javascript·spring·springsecurity