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);
}
相关推荐
梦想CAD控件1 分钟前
网页端CAD的图形选择、编辑与夹点操作教程
前端·javascript·node.js
妙码生花6 分钟前
从 PHP 到 AI + Golang,程序员自救转型手记(五十二):管理员权限检查中间件,AI 随意放行预闯大祸
前端·后端·go
月月大王的3D日记16 分钟前
Three.js 入门系列(8):六种光源全解析 —— 关灯了,开光!
前端·javascript
属于自己的天空1 小时前
每天省去重复 Prompt:我用 Skills 把 CRUD 生成标准化了
前端·后端
卡卡罗特AI1 小时前
GitHub怎么用?零基础,保姆级使用教程-上!建议收藏
前端·后端·github
自然 醒1 小时前
v-tooltip自定义指令封装
前端·javascript·vue.js
因_崔斯汀1 小时前
Three.js 3D 热力云图效果实现
前端·three.js
爱勇宝2 小时前
人到了一定年纪,才会看懂这些人性真相
前端·后端
梓䈑2 小时前
【用 Vibe Coding 实现的 C++17 在线判题系统】前端开发 + Web 自动化测试
前端·c++·ai编程