当前位置:网站首页>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
边栏推荐
- R language ggplot2 visualization: visualize the scatter diagram and add text labels to the data points in the scatter diagram, using geom of ggrep package_ text_ The rep function avoids overlapping da
- Poj1860 currency exchange solution
- Org.apache.ibatis.exceptions.toomanyresultsexception
- Poj3259 wormhole solution
- PHP generates random numbers (nickname random generator)
- TS扫盲大法-基础篇
- Poj3268 shortest path solution
- 长封闭期私募产品再现 业内人士看法各异
- C语言:随机生成数+归并排序
- 微信小程序中自定义模板
猜你喜欢

Customized template in wechat applet

Denial of service DDoS Attacks

30天刷题计划(二)

Humiliation, resistance, reversal, 30 years, China should win Microsoft once
![[security] read rfc6749 and understand the authorization code mode under oauth2.0](/img/dc/e6d8626195b2e09a6c06050a9b552e.jpg)
[security] read rfc6749 and understand the authorization code mode under oauth2.0

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

【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式

在 Kubernetes 中部署应用交付服务(第 1 部分)

30 day question brushing plan (III)

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
随机推荐
111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server
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
SQL daily practice (Niuke new question bank) - day 4: advanced operators
Product Manager: job responsibility table
Li Kou sword finger offer 51. reverse order pairs in the array
7.依赖注入
Dojnoip201708 cheese solution
数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
Tutorial on the principle and application of database system (059) -- MySQL exercise questions: operation questions 1-10 (III)
Humiliation, resistance, reversal, 30 years, China should win Microsoft once
图的遍历(BFS&&DFS基础)
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
Debezium series: major changes and new features of 2.0.0.beta1
严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数
多线程与高并发(三)—— 源码解析 AQS 原理
30天刷题训练(一)
Customized template in wechat applet
Poj1860 currency exchange solution
30 day question brushing plan (IV)
After finishing, help autumn move, I wish you call it an offer harvester