文件流导出文件

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";
  }
}
相关推荐
阿蒙Amon5 小时前
TypeScript学习-第7章:泛型(Generic)
javascript·学习·typescript
睡美人的小仙女1275 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
fanruitian5 小时前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo5 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk5 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
摘星编程6 小时前
React Native + OpenHarmony:Timeline垂直时间轴
javascript·react native·react.js
2501_944525547 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
jin1233227 小时前
React Native鸿蒙跨平台完成剧本杀组队详情页面,可以复用桌游、团建、赛事等各类组队详情页开发
javascript·react native·react.js·ecmascript·harmonyos
李白你好7 小时前
Burp Suite插件用于自动检测Web应用程序中的未授权访问漏洞
前端
经年未远8 小时前
vue3中实现耳机和扬声器切换方案
javascript·学习·vue