download.js 692 B

123456789101112131415161718
  1. const axios = require('@/utils/request').default
  2. export default async function download({ file_name, file_path }) {
  3. const res = await axios({
  4. url: file_path,
  5. method: 'get',
  6. responseType: 'blob'
  7. })
  8. const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
  9. const a = window.document.createElement('a')
  10. const href = window.URL.createObjectURL(blob)
  11. const disposition = res.headers['content-disposition']
  12. const filename = file_name || window.decodeURIComponent(new RegExp('filename=(.*)').exec(disposition)[1])
  13. a.href = href
  14. a.download = filename
  15. a.click()
  16. window.URL.revokeObjectURL(href)
  17. }