文件流导出文件

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";
  }
}
相关推荐
菩提小狗1 小时前
第2天:基础入门-Web应用&架构搭建&漏洞&HTTP数据包&代理服务器|小迪安全笔记|网络安全|
前端·安全·架构
ctrigger1 小时前
监理工程师考试题型有哪些?4科题型+分值表
大数据·javascript·算法
咖啡の猫1 小时前
Python集合生成式
前端·javascript·python
dazzle1 小时前
计算机视觉处理(OpenCV基础教学(六):基于HSV颜色空间的目标颜色识别)
javascript·opencv·计算机视觉
2501_946233891 小时前
Flutter与OpenHarmony我的作品页面实现
android·javascript·flutter
QT 小鲜肉2 小时前
【Linux命令大全】001.文件管理之mtoolstest命令(实操篇)
linux·运维·前端·笔记·microsoft
holeer2 小时前
React UI组件封装实战——以经典项目「个人博客」与「仿手机QQ」为例
前端·javascript·react.js·ui·前端框架·软件工程
chilavert3182 小时前
技术演进中的开发沉思-277 AJax :Calendar
前端·javascript·microsoft·ajax
chilavert3182 小时前
技术演进中的开发沉思-276 AJax : Menu
javascript·ajax·交互
debug 小菜鸟2 小时前
搭建web 环境的那些事
前端