Vue 下载文件

64 阅读1分钟
import axios from "axios";
import { Notification, MessageBox, Message, Loading } from "element-ui";
export function updateFileName(url, params, filename) {
  return service
    .post(url, params, {
      responseType: "blob",
    })
    .then(data=> {
      const content = data
      const blob = new Blob([content])
      const link = document.createElement('a')
      link.download = filename
      link.style.display = 'none'
      link.href = URL.createObjectURL(blob)
      document.body.appendChild(link)
      link.click()
      URL.revokeObjectURL(link.href)
      document.body.removeChild(link)
    }).catch((r) => {
      console.error(r);
      Message.error("下载文件出现错误,请联系管理员!");
      downloadLoadingInstance.close();
    });
}