注:若有不明白的地方可以先看下方知识点哦~
<div>
<a class="el-button el-button--primary el-button--mini" href='https://file2.clipworks.com/4c217acc09226795a7ebb8461ddf548f/20220726/469c1056-749d-4c47-819e-35f91c70e163' download="视频教程模板">模板下载a>
div>
按照如上代码,当点击时并不会下载而是跳转到当前url!
是因为download属性具有同源策略,若是不同源不会下载而是跳转!
既然a标签下载存在同源策略,我们就依据远程链接创建一个本地url;
<template>
<div style="margin-top:20px">
<a class="el-button el-button--primary el-button--mini" :href='url' download="视频教程模板" id='downloadBtn' target='_blank'>模板下载</a>
</div>
</template>
<script>
export default {
data(){
return{
url:undefined
}
},
created(){
this.getLocalUrl()
},
methods:{
getLocalUrl(){
// 创建本地url
const url = 'https://file2.clipworks.com/4c217acc09226795a7ebb8461ddf548f/20220726/469c1056-749d-4c47-819e-35f91c70e163'
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.responseType = "blob"
xhr.onload = ()=>{
console.log('res', xhr.response)
this.url = URL.createObjectURL(xhr.response)
}
xhr.onerror = e =>{
console.log('err', e)
}
xhr.send()
}
}
}
</script>