当前位置:网站首页>a标签_文件下载(download属性)
a标签_文件下载(download属性)
2022-07-28 12:50:00 【乖女子@@@】
需求描述
- 需求:当点击按钮时,可以下载模板
- 下载地址是一个远程链接—>https://file2.clipworks.com/4c217acc09226795a7ebb8461ddf548f/20220726/469c1056-749d-4c47-819e-35f91c70e163
实现过程
注:若有不明白的地方可以先看下方知识点哦~
实现
<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>
- a标签样式采用的是elementUI中按钮的样式
问题
若是href属性值为服务器链接而非本地链接,当点击时并不会下载而是跳转到当前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>
知识点
知识点1-a标签的download属性
- download属性是h5新增的a标签属性!
- download属性作用是:指示浏览器下载 URL 而不是导航到URL; download属性值 将在下载保存过程中作为预填充的文件名;
- 注意点:
- 若url为远程链接,此属性存在同源策略—>不同源的url不会下载而是直接跳转;
- 兼容性不太好(ie不支持)
知识点2-原生http请求
知识点3-创建本地url
边栏推荐
- Generation of tables and contingency tables (cross tables) of R language factor data: use the summary function to analyze the list, view the chi square test results, and judge whether the two factor v
- DDoS protection with iptables
- DOJP1520星门跳跃题解
- R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(使用参数xlim和ylim将标签添加到可视化图像的特定区域、指定标签线段并添加箭头)
- 严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数
- Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
- word打字时后面的字会消失是什么原因?如何解决?
- Li Kou sword finger offer 51. reverse order pairs in the array
- Can second uncle cure young people's spiritual internal friction?
- 酷炫操作预热!代码实现小星球特效
猜你喜欢

Cool operation preheating! Code to achieve small planet effect

111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server

111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误

SQL每日一练(牛客新题库)——第4天:高级操作符

Customized template in wechat applet

Can second uncle cure young people's spiritual internal friction?

Org.apache.ibatis.exceptions.toomanyresultsexception

Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function

Three men "running away" from high positions in the mobile phone factory

不用Swagger,那我用啥?
随机推荐
数据库系统原理与应用教程(059)—— MySQL 练习题:操作题 1-10(三)
Poj3268 shortest path solution
面经整理,助力秋招,祝你称为offer收割机
Is azvudine, a domestic oral new coronal drug, safe? Expert authority interpretation
P1797重型运输 题解
7.依赖注入
Some thoughts on.Net desktop development
[Architecture] reading notes of three micro service books with high scores
安全保障基于软件全生命周期-NetworkPolicy应用
【架构】评分较高的三本微服务书籍的阅读笔记
Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open
Understanding of stack and practical application scenarios
Three men "running away" from high positions in the mobile phone factory
Uva1599 ideal path problem solution
SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
Can second uncle cure young people's spiritual internal friction?
R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)
Beyond istio OSS -- current situation and future of istio Service Grid
Tutorial on the principle and application of database system (061) -- MySQL exercise: operation questions 21-31 (V)
DOJNOIP201708奶酪题解