前端下载get,post方法

import axios from "@/api";
import {elMessagefun} from "@/assets/common/utils";
import {requestCode} from "@/assets/common/common";

function commonExport(res,name){
  const url = window.URL.createObjectURL(new Blob([res], {type: "application/vnd.ms-excel"}))
  const a = document.createElement('a');
  a.href = url;
  a.setAttribute('download', name);
  a.click();
  a.remove();
}

export const postExport = async function (url, data, name){
  let res = await axios.post(url, data, {responseType: 'blob'})
  if (res.code&&(res.code !== requestCode)) {
    elMessagefun(res.msg);//提示的方法
    return
  };
  commonExport(res,name)
}

export const getExport = async function (url, config,name){
  let res = await axios.get(url, config)
  commonExport(res,name)
}

你可能感兴趣的:(小业务,js,前端,javascript,开发语言)