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);
}
相关推荐
掘金0115 分钟前
EmbedPDF Vue 版 完整正文文档 全网首发
前端
OpenTiny社区16 分钟前
操作ArkTS页面跳转及路由相关心得
前端·typescript·web·opentiny
xiaohua0708day17 分钟前
Lodash库
前端·javascript·vue.js
huakoh17 分钟前
Claude Code 从零到上手指南:国产工具链复现80% Agent能力,DeepSeek+LangChain实战
前端
Ankkaya20 分钟前
浏览器插件接入 Google 登录
前端
Asmewill21 分钟前
DeepAgents学习笔记一(构建深度多智能体)
前端
万物皆对象66622 分钟前
切换路由时页面空白问题(vue3)
前端·vue.js·typescript
突然好热22 分钟前
TS 调试技巧
前端·javascript·typescript
h64648564h23 分钟前
Flutter 国际化(i18n)全指南:一键切换中/英/日多语言
前端·javascript·flutter
令人头秃的代码0_024 分钟前
AI时代下,如何做原子代码拆分
前端