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:')
這樣做的原因是瀏覽器會認(rèn)為在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 Overflowhttps://stackoverflow.com/questions/67765238/mixed-content-the-page-at-was-loaded-over-https-but-requested-an-insecure-resour文章來源:http://www.zghlxwxcb.cn/news/detail-719449.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-719449.html
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
到了這里,關(guān)于跨域問題問題小結(jié)(vue3)---處理http文件鏈接下載失敗的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!