vue3中,下载模板(三)——前端本地下载附件

效果

在这里插入图片描述

1、页面

index.vue

<el-button type="primary" @click="fileDownload">下载模板</el-button>
<script setup>
import {downFile} from '@src/utils/util'
// 下载模板
const fileDownload = async () => {
  downFile('Plan-Import模板.xlsx')
}
</script>
2、模板

在这里插入图片描述

3、file流文件处理

src\utils\util.js

//本地文件下载  下载的模板文件放在public下的file文件夹中
export const downFile = (fileName) => {
  const anchor = document.createElement('a')
  anchor.href = `${process.env.BASE_URL}static/file/${fileName}`
  anchor.setAttribute('download', fileName)
  anchor.innerHTML = 'downloading...'
  anchor.style.display = 'none'
  document.body.appendChild(anchor)
  setTimeout(() => {
    anchor.click()
    document.body.removeChild(anchor)
    setTimeout(() => {
      self.URL.revokeObjectURL(anchor.href)
    }, 250)
  }, 66)
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐