背景:vue3项目点击下载按钮下载文件,使用在页面添加标签的方式会导致文件(图片/pdf)直接打开而不是下载。a标签的方法还是会打开预览consturl='http://192.168.60.59:8888/fayuan/head/4b33a2a1-3911-4586-9878-50373a1fb852.jpg'consta=document.createElement('a')a.setAttribute('download','')a.setAttribute('href',url)a.click()//自执行点击事件解析:setup(){constdown=()=>{consturl='http://file.fayuan.com/tiaojie/wechatImg/website/ali-mini-operation.pdf'constfilename='操作指南'constx=newXMLHttpRequest()x.open('GET',url,true)x.responseType='blob'x.onload=e=>{//将创建一个包含表示参数中给定对象的URL的DOMString。此URL的生命周期与创建它的窗口中的文档相关联。这个新的URL对象代表指定的File对象或Blob对象。consturl=window.URL.createObjectURL(x.response)consta=document.createElement('a')a.href=urla.download=文件名a.click()}x.send()}return{down}}
