import { Message } from '@arco-design/web-vue'

export const downloadFile = (url: string, name: string) => {
  window
    .fetch(url)
    .then(
      (res: any) => {
        if (res.status === 200) {
          return res.blob()
        }
        return false
      },
      error => {
        Message.error(`文件下载错误${error}`)
      }
    )
    .then(blob => {
      const a = document.createElement('a')
      document.body.appendChild(a)
      a.style.display = 'none'
      const downloadUrl = window.URL.createObjectURL(blob)
      a.href = downloadUrl
      a.download = name
      a.click()
      document.body.removeChild(a)
      window.URL.revokeObjectURL(downloadUrl)
    })
}
