文件流下载需创建一个xmlhttp请求,请求格式为blob
<el-button class="title-import" @click="download" type="text">下载导入模板</el-button>
export default {
name: "",
data() {
return {
}
},
methods: {
download() {
// 例子: this.downUrl为文档域名,可根据需求变化
// this.templateUrl = "https://shanghai.aliyuncs.com/custom/document/141804082184930.xlsx"
this.templateUrl = 'https://' + this.downUrl + '/templates/导入模板.xlsx'
let fileName = '导入模板'
// console.log(this.templateUrl)
this.downCommon(this.templateUrl,fileName)
},
downCommon(url,fileName){
try {
return new Promise((resolve, reject) => {
//通过请求获取文件blob格式
let xmlhttp = new XMLHttpRequest()
xmlhttp.open('GET', url, true)
// xmlhttp.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
xmlhttp.responseType = 'blob'
xmlhttp.onload = function () {
if (this.status == 200) {
let url = window.URL.createObjectURL(this.response)
let a = document.createElement('a')
a.href = url
a.download = fileName
a.click()
resolve(this.response)
} else {
reject(this.status)
}
}
xmlhttp.send()
})
} catch (e) {
alert('下载异常!')
}
},
}
}