文件流导出文件

javascript 复制代码
// 下载excel
export const downloadFile = (fileStream, fileName) => {
  if (window.navigator.msSaveBlob) {
    try {
      window.navigator.msSaveBlob(fileStream, fileName);
    } catch (e) {
      console.log(e);
    }
  } else {
    const url = window.URL || window.webkitURL;
    const bUrl = url.createObjectURL(fileStream);
    let a = document.createElement("a");
    a.style.display = "none";
    a.href = bUrl;
    a.download = decodeURIComponent(fileName);
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    url.revokeObjectURL(bUrl);
  }
};

使用downloadFile(res, `默认文件.xlsx`);

res是后端返回文件流,默认文件.xlsx是文件名 .xlsx是后缀名

javascript 复制代码
// 获取文件类型
export function getFileType(file) {
  const type = file.type || "";
  const name = file.name || "";
  if (type.includes("word")) {
    return "doc";
  } else if (type.includes("excel") || type.includes("spreadsheetml.sheet")) {
    return "excel";
  } else if (type === "text/plain") {
    return "txt";
  } else if (type === "application/pdf") {
    return "pdf";
  } else if (type === "text/html") {
    return "html";
  } else if (type === "text/markdown" || name.includes(".md")) {
    return "md";
  } else {
    return "other";
  }
}
相关推荐
IT_陈寒18 小时前
Redis那个过期删除策略,差点让我线上崩了
前端·人工智能·后端
jianqiang.xue18 小时前
Espressif-IDE进入维护模式:乐鑫把ESP32开发的宝押给VS Code
前端·css·ide
特创数字科技19 小时前
纯本地离线运行的文档脱敏工具(技术分享)
前端·驱动开发·python
心之语歌19 小时前
uni-app uts原生插件开发:uts 与 Kotlin 交互踩坑实录(附解决方案)
前端·后端
再吃一根胡萝卜19 小时前
DSH 插件数据链路:Host 管数据,Client 只管画
前端
feixing_fx19 小时前
CSS 变量(Custom Properties):构建可扩展的主题切换系统
前端·css·前端框架·html·css3
十有八七20 小时前
手搓 Agent(一):先有大脑——大模型只会做一件事
前端·人工智能·后端
锋行天下20 小时前
「踩坑实录」给 LangGraph Agent 接两个 FastMCP,我被 langchain 偷偷改了工具名
前端·人工智能·后端
跳跳的小星星21 小时前
自研组件】Vue2 + ElementUI 通用列表页封装 wkjp-list-page,一套配置搞定搜索、动态表格、分页、增删改
前端
tryCbest21 小时前
Vue3—— 第 18 篇:Vue 2 到 Vue 3 迁移指南——升级避坑全记录
前端·vue.js