// 下载导入模板
export const downloadTemplate = (herf, fileName) => {
const a = document.createElement('a')
a.href = herf
a.download = fileName
a.style.display = 'none'
document.body.appendChild(a)
a.click()
a.remove()
}
<script>
import { downloadTemplate } from '路径'
export default {
data() {
return {
},
methods: {
momentDate,
downloadTemplate, // 文件下载
downLoad(){
this.downloadTemplate(文件路径, 下载时文件名称)
}
}
上面一种方法可能存在无法更改名称
所以我们可以使用xml或者axios来下载,下面为xml下载方法
/**
* 下载导入模板
* herf 下载路径
* fileName 下载文件名称
*/
export const downloadTemplate = (herf: string, fileName: string) => {
const xml = new XMLHttpRequest()
xml.open('GET', herf, true)
xml.responseType = 'blob'
xml.onload = function () {
const url = window.URL.createObjectURL(xml.response)
const a = document.createElement('a')
a.href = url
a.download = fileName
a.click()
}
xml.send()
}