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);
}
相关推荐
合作小小程序员小小店3 小时前
web开发,在线%超市销售%管理系统,基于idea,html,jsp,java,ssh,sql server数据库。
java·前端·sqlserver·ssh·intellij-idea
不爱学英文的码字机器3 小时前
重塑 Web 性能:用 Rust 与 WASM 构建“零开销”图像处理器
前端·rust·wasm
浩星4 小时前
react的框架UmiJs(五米)
前端·javascript·react.js
子醉6 小时前
推荐一种适合前端开发使用的解决本地跨域问题的办法
前端
Niyy_6 小时前
前端一个工程构建多个项目,记录一次工程搭建
前端·javascript
xiangxiongfly9157 小时前
CSS link标签
前端·css
快乐非自愿7 小时前
常用设计模式:工厂方法模式
javascript·设计模式·工厂方法模式
岁月宁静8 小时前
AI 多模态全栈应用项目描述
前端·vue.js·node.js
十年磨一剑~8 小时前
html+js开发一个测试工具
javascript·css·html