当前位置:网站首页>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
边栏推荐
- word打字时后面的字会消失是什么原因?如何解决?
- [dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing
- DXF读写:标注样式组码中文说明
- Li Kou sword finger offer 51. reverse order pairs in the array
- POJ1860货币兑换题解
- I miss the year of "losing" Li Ziqi
- 拒绝服务 DDoS 攻击
- Customized template in wechat applet
- R language Visual scatter diagram, geom using ggrep package_ text_ The repl function avoids overlapping labels between data points (add labels to specific areas of the visual image using the parameter
- Lyscript get previous and next instructions
猜你喜欢

30 day question brushing plan (II)

SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response

Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022

Today's sleep quality record 75 points

安全保障基于软件全生命周期-Istio的授权机制

不用Swagger,那我用啥?

Blue Bridge Training (additional interview questions) day 7

7.依赖注入

30 day question brushing plan (IV)

30 day question brushing plan (III)
随机推荐
You have to apologize if you get involved in the funny shop?
用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
使用 IPtables 进行 DDoS 保护
了解虚拟列表背后原理,轻松实现虚拟列表
Tutorial on the principle and application of database system (058) -- MySQL exercise (2): single choice question
R语言ggplot2可视化:可视化散点图并为散点图中的数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点标签互相重叠(自定义指定字体类型font family)
严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数
P1797 heavy transportation problem solution
C language: quick sorting of sequential storage structure
关于栈的理解以及实际应用场景
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的边框颜色
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
Tutorial on the principle and application of database system (060) -- MySQL exercise: operation questions 11-20 (IV)
我秃了!唯一索引、普通索引我该选谁?
DXF读写:标注样式组码中文说明
UVA1599理想路径题解
111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误
对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
leetcode(442)数组中重复的数据