当前位置:   article > 正文

a标签下载文档 a下载文档失败问题 跨域调用_使用a标签下载doc文档报错

使用a标签下载doc文档报错

可以通过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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/63567?site
推荐阅读
相关标签
  

闽ICP备14008679号