跨域问题问题小结(vue3)---处理http文件链接下载失败

const fileUrl = 'http://......'
const replacedUrl = fileUrl.replace(/^http:/, 'https:')
const ele = document.createElement('a')
ele.setAttribute('href', replacedUrl)
ele.setAttribute('download', replacedUrl.split('/')[replacedUrl.split('/').length - 1])
ele.click()

请求后端 提供的链接居然是一个http的文件链接

首先对这个地址做了正则判断替换成https

fileUrl.replace(/^http:/, 'https:')

这样做的原因是浏览器会认为在https下打开的http链接是不安全操作,所以会进行拦截,或者访问不成功

替换后运行报错

Mixed Content: The site at 'https://.....cn/' was loaded over a secure connection, but the file at 'https://.....cn/excel/20230612162249172.xlsx' was redirected through an insecure connection. This file should be served over HTTPS. This download has been blocked. See https://blog.chromium.org/2020/02/protecting-users-from-insecure.html for more details.

错误信息告诉我们混合请求(两个地址域名不一致)是被浏览器禁止,处理办法如下:

参考:servlets - Mixed Content The page at was loaded over HTTPS but requested an insecure resource This request has been blocked the content must be served over HTTPS - Stack Overflowicon-default.png?t=N7T8https://stackoverflow.com/questions/67765238/mixed-content-the-page-at-was-loaded-over-https-but-requested-an-insecure-resour

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值