可以通过a标签下载文档
a中有一个download属性
这个属性可以为空,如果写入的话这是写下载文档的名字
a标签默认下载是在同一个域内,如果跨域的话下载会失败,可能变成预览
出现的问题
from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
可能原因
在服务端禁止掉其他地址的访问,你这个地址被设置为其他域
需要后台去设置
其他解决方法:
要么最后打包在同一域内
要么通过接口调用
调用的代码:
fetch(href).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址const a = document.createElement('a')a.href = URL.createObjectURL(blob)console.log(a.href)a.download = fileName // 下载文件的名字document.body.appendChild(a)a.click()URL.revokeObjectURL(a.href) // 释放URL 对象document.body.removeChild(a)