文件流导出文件

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";
  }
}
相关推荐
朴shu2 分钟前
揭秘高性能协同白板:轻松实现多人实时协作(一)
前端·设计模式·架构
wyjcxyyy3 分钟前
polar靶场-MISC,WEB(中等)
前端·chrome
2301_816073835 分钟前
SELinux 学习笔记
linux·运维·前端
秋天的一阵风6 分钟前
😱一行代码引发的血案:展开运算符(...)竟让图表功能直接崩了!
前端·javascript·vue.js
Hilaku10 分钟前
npm scripts的高级玩法:pre、post和--,你真的会用吗?
前端·javascript·vue.js
申阳18 分钟前
Day 12:09. 基于Nuxt开发博客项目-使用NuxtContent构建博客模块
前端·后端·程序员
合作小小程序员小小店27 分钟前
web网页开发,在线短视频管理系统,基于Idea,html,css,jQuery,java,springboot,mysql。
java·前端·spring boot·mysql·vue·intellij-idea
n***293235 分钟前
前端动画性能优化,减少重绘重排
前端·性能优化
mCell38 分钟前
React 如何处理高频的实时数据?
前端·javascript·react.js
随笔记40 分钟前
HbuilderX载入项目,运行后唤起微信开发者工具,提示:Error: Fail to open IDE,唤醒不起来怎么办
javascript·微信小程序·uni-app