我的前端坑
  • 关于本书
  • CSS JS 常用
    • 常用 CSS 样式
    • 坑爹的 CSS
    • sass 入门
    • canvas 总结
    • 常用 JS 函数
    • 表单和 FormData 对象
    • 水平滑动 tab 居中
    • js 中的 this
    • sse 和 fetch
    • js 原型链与 class 类
  • TypeScript
    • TS 概念
    • interface 与 type
    • interface 接口
    • Ts 配合 ESLint 和 prettier
  • 小程序
    • 常用小程序代码
  • VUE
    • VUE2 小技巧
    • VUE-CLI 中的 NODE_ENV
  • VUE3
    • VUE3 自行构建 sfc 遇到的坑
    • VUE3 v-model 的实现
    • VUE3 使用总结
    • VUE3 ref
  • vite
    • vite
  • http 请求
    • 前端实现下载
    • cors 跨域请求
    • windows hosts 文件
    • err_blocked_by_client 错误
  • 前端工具
    • npm 和 Node
      • 常见问题
      • npmTips
      • package.json 与 package-lock.json
      • npx
      • exports 与 module.exports
      • ESLint
    • VIM
      • vim 常用
    • Git
      • Git 常用命令
      • Git 小 tips
    • express
  • 后端工具
    • mysql 常见问题
    • docker 常见问题
    • docker
  • java
    • java 常用
    • lambda 表达式
    • java 字符串
    • java 泛型
    • java 反射
    • intellij IDEA
    • 多态
    • java 包管理
    • sql 查询语言
    • java 反射
    • java 异常
    • java 集合
    • spring
  • 命令行
    • 命令行 常用
  • 专利撰写 ppt
  • 后台简述
Powered by GitBook
On this page

Was this helpful?

  1. http 请求

前端实现下载

有时候后台管理需要导出功能,a 标签默认会打开新页面,此方法使用 dom 操作在当前页 面创建下载链接并点击下载

 //下载任务文件事件 url 为下载地址
  function downloadFiles(url, name) {
    const aLink = document.createElement('a') //创建a链接
    aLink.style.display = 'none'
    aLink.href = url
    aLink.download = name
    document.body.appendChild(aLink)
    aLink.click()
    document.body.removeChild(aLink) //点击完成后记得删除创建的链接
  },
 //当后台返回的不是下载地址,而是文件的 blob 格式对象时
 //注意 blobObj 为 blob 格式对象!!!
 //  方法1:
 function downloadFiles(blobObj,fileName){
   const tempBlob = URL.createObjectURL(blobObj)
   const aLink = document.createElement('a')
   aLink.href = tempBlob
   aLink.download = fileName
   aLink.click()
   URL.revokeObjectURL(tempBlob)
 }
//  方法2:
 function downloadFiles(blobObj, fileName) {
   const reader = new FileReader()
   reader.readAsDataURL(blobObj)
   reader.onload = (e) => {
     const aLink = document.createElement('a')
     aLink.style.display = 'none'
     aLink.href = e.target.result
     aLink.download = 'fileName'
     document.body.appendChild(aLink)
     aLink.click()
     document.body.removeChild(aLink) //点击完成后记得删除创建的链接
   }
 }

举例,下载 application/vnd.ms-excel 格式文件

当请求返回的是 application/vnd.ms-excel 类型的数据时,发送 XMLHttpRequest 请求时,注意设置 responseType 为 blob

// 1 使用原生 XMLHttpRequest 发送请求
const xhr = new XMLHttpRequest();
xhr.open("GET", "/path/to/excel/template", true);
// 设置responseType属性的值,告诉浏览器如何解读返回的数据
xhr.responseType = "blob";
// load 事件表示服务器传来的数据接收完毕
xhr.onload = function (e) {
  if (this.status === 200) {
    // 如果返回的blob数据类型是 application/vnd.ms-excel
    if (xhr.response.type.includes("application/vnd.ms-excel")) {
      const blob = new Blob([xhr.response], {
        type: "application/vnd.ms-excel;charset=UTF-8",
      });
      const tempBlob = URL.createObjectURL(blob);
      const aLink = document.createElement("a");
      aLink.href = tempBlob;
      aLink.download = "模板文件";
      aLink.click();
      URL.revokeObjectURL(tempBlob);
    } else if (xhr.response.type.includes("application/json")) {
      // 如果返回的blob 数据类型是 application/json 则代表下载有问题,我们显示一下信息
      const reader = new FileReader();
      reader.readAsText(this.response, "utf-8");
      reader.onload = () => {
        const res = JSON.parse(reader.result);
        alert(res.msg);
      };
    }
  }
};
xhr.send();

// 2 使用 axios 发送请求
axios({
  method: "get",
  url: "/path/to/excel/template",
  responseType: "blob",
}).then((res) => {
  const blob = new Blob([res.data], {
    type: "application/vnd.ms-excel;charset=UTF-8",
  });
  const tempBlob = URL.createObjectURL(blob);
  const aLink = document.createElement("a");
  aLink.href = tempBlob;
  aLink.download = "模板文件";
  aLink.click();
  URL.revokeObjectURL(tempBlob);
});
Previoushttp 请求Nextcors 跨域请求

Last updated 2 years ago

Was this helpful?