封装导出EXCEL文件请求

185 阅读1分钟

参数

url :请求路径

data: 请求参数

fileName: 导出文件名字

调用方式

import { exportFilePost } from '../../../../utils/exportFile';
let url = 'xxxx/api';      
let data = this.deriveData;      
let name = '我是导出文件';      
exportFilePost(url, data, name);

POST导出

export function exportFilePost(url, data, fileName) {  fetch(url, {    method: 'POST',    headers: { 'Content-Type': 'application/json;charset=utf-8' },    body: JSON.stringify(data),  })    .then((res) => {      return res.blob();    })    .then((res) => {      function downloadFileByBlob(blobUrl, filename) {        const a = document.createElement('a');        a.download = filename;        a.style.display = 'none';        a.href = blobUrl;        document.body.appendChild(a);        a.click();        document.body.removeChild(a);      }      const blobContent = new Blob([res], { type: 'application/octet-stream' });      const blobUrl = window.URL.createObjectURL(blobContent);      let name = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');      downloadFileByBlob(blobUrl, `${name}` + `${fileName}` + '.xlsx');    });}