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);
}
相关推荐
鹏程十八少2 分钟前
12. Android 协程通关秘籍:31 道资深工程师面试题精讲
前端·后端·面试
Dlrb121114 分钟前
C语言-字符串指针与函数指针
java·c语言·前端
PBitW19 分钟前
组件封装注意事项
前端·vue.js
weiggle25 分钟前
Android 输入事件分发流程:从物理触控到 Activity 的完整旅程
前端
yingyima28 分钟前
开发者必备在线工具集合 2025:实战案例解析
前端
前端毕业班29 分钟前
面试官:实现一个带类型约束的 EventEmitter
前端·面试
卷帘依旧33 分钟前
SPA 中的 Hash 和 History 模式
前端
用户44455436542636 分钟前
AndroidAutoSize使用时遇到的特麻烦bug
前端
茉莉玫瑰花茶40 分钟前
LangGraph 入门教程:构建 AI 工作流 [ 案例三 ]
前端·人工智能·python
scan72444 分钟前
pydantic格式输出
服务器·前端·javascript