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);
}
相关推荐
当时只道寻常5 分钟前
Vue3 集成 NProgress 进度条:从入门到精通
前端·vue.js
kyriewen6 分钟前
React性能优化:从“卡成狗”到“丝般顺滑”的5个秘诀
前端·react.js·性能优化
米丘6 分钟前
Vue 3.x 单文件组件(SFC)模板编译过程解析
前端·vue.js·编译原理
helloweilei8 分钟前
Web Streams 简介
前端·javascript
悟空瞎说8 分钟前
Flutter热更新 Shorebird CodePush 原理、实现细节及费用说明
前端·flutter
didadida2629 分钟前
从“不存在”的重复请求,聊到 Web 存储的深坑
前端
xiaominlaopodaren10 分钟前
Three.js 渲染原理-透明渲染为什么这么难
前端
米丘11 分钟前
vue3.x 内置指令有哪些?
前端·vue.js
米丘12 分钟前
Vue 3.x 模板编译优化:静态提升、预字符串化与 Block Tree
前端·vue.js·编译原理