使用nginx解決前端js下載跨域問(wèn)題
背景
訂單系統(tǒng)增加附件預(yù)覽 , 下載的功能,但是這個(gè)附件是客戶(hù)推單時(shí)推送過(guò)來(lái)的,文件連接是類(lèi)似oss連接,但是是客戶(hù)的域名,所以導(dǎo)致跨域問(wèn)題.
前端是vue項(xiàng)目
解決過(guò)程
1.面向百度編程
先百度了一番
基本都是說(shuō)用 blob 這個(gè)我真不太了解,但是在項(xiàng)目里找到了別人寫(xiě)的下載方法,也是用的blob,但是根本解決不了跨域問(wèn)題.
2.擺爛
百度一番沒(méi)找到解決辦法,于是打算擺爛
附件預(yù)覽界面大約長(zhǎng)這樣子,直接鼠標(biāo)在鏈接上面右鍵另存為可以下載附件
于是改造了一下原來(lái)的下載方法 增加個(gè)友好提示
downloadFile(url) { const fileName = this.getFilenameFromUrl(url); // 文件名 const xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'blob'; const _this = this xhr.onload = function () { if (this.status === 200) { const blob = new Blob([this.response], {type: 'application/octet-stream'}); const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; document.body.appendChild(link); link.click(); document.body.removeChild(link); } }; // 增加友好提示 下載失敗讓用戶(hù)右鍵另存為 xhr.onerror = function (){ _this.$modal.msgError("下載失敗,請(qǐng)嘗試在鏈接上使用右鍵另存為"); } xhr.send(); }
3.重新出發(fā)
增加友好提示以后,同事都要被我笑死了,這種方式簡(jiǎn)直low爆了
于是我想到用nginx反向代理來(lái)解決這個(gè)問(wèn)題
繼續(xù)百度
找到一篇地址
用本地nginx試了一下
location /download { if ($query_string ~* ^(.*)url=(.*)$){ set $imageUrl $2; proxy_pass $imageUrl; } }
修改下載方法
downloadFile(url) { const fileName = this.getFilenameFromUrl(url); // 文件名 const xhr = new XMLHttpRequest(); const newUrl = 'http://localhost/download?url=' + url xhr.open('GET', newUrl, true); xhr.responseType = 'blob'; const _this = this xhr.onload = function () { if (this.status === 200) { const blob = new Blob([this.response], {type: 'application/octet-stream'}); const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; document.body.appendChild(link); link.click(); document.body.removeChild(link); } }; xhr.onerror = function (){ _this.$modal.msgError("直接下載失敗,請(qǐng)嘗試在鏈接上使用右鍵另存為"); } xhr.send(); }
居然成功了,可以下載文件, 于是修改了一下 上測(cè)試服務(wù)器
最終版js
downloadFile(url) { const fileName = this.getFilenameFromUrl(url); // 文件名 const xhr = new XMLHttpRequest(); const newUrl = window.location.protocol + '//' + window.location.hostname + '/download?url=' + url xhr.open('GET', newUrl, true); xhr.responseType = 'blob'; const _this = this xhr.onload = function () { if (this.status === 200) { const blob = new Blob([this.response], {type: 'application/octet-stream'}); const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; document.body.appendChild(link); link.click(); document.body.removeChild(link); } }; xhr.onerror = function (){ _this.$modal.msgError("直接下載失敗,請(qǐng)嘗試在鏈接上使用右鍵另存為"); } xhr.send(); }
4.新問(wèn)題
上測(cè)試服務(wù)器后發(fā)現(xiàn)按下載按鈕還是失敗,發(fā)現(xiàn)居然報(bào)502
翻了一下nginx的報(bào)錯(cuò)日志
no resolver defined to resolve ........
百度了一下
原來(lái)nginx代理的地址如果是域名的話(huà) 需要增加dns解析 nginx并不會(huì)直接用系統(tǒng)的dns
location /download { resolver 你的dns; if ($query_string ~* ^(.*)url=(.*)$){ set $imageUrl $2; proxy_pass $imageUrl; } }
設(shè)置完 resolver 后 再試 果然成功了
以上就是使用nginx解決前端js下載跨域問(wèn)題的詳細(xì)內(nèi)容,更多關(guān)于nginx解決js下載跨域的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx中透?jìng)骺蛻?hù)端真實(shí)IP的技巧
為了記錄日志、限制訪問(wèn)或進(jìn)行其他基于?IP?地址的操作,獲取客戶(hù)端的真實(shí)?IP?地址非常重要,本文就來(lái)詳細(xì)的介紹一下Nginx中透?jìng)骺蛻?hù)端真實(shí)IP的技巧,感興趣的可以了解一下2024-08-08Nginx使用反向代理實(shí)現(xiàn)負(fù)載均衡過(guò)程解析
這篇文章主要介紹了Nginx使用反向代理實(shí)現(xiàn)負(fù)載均衡過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09分割nginx日志的實(shí)現(xiàn)(避免日志多大)
nginx默認(rèn)沒(méi)有提供對(duì)日志文件的分割功能,所以隨著時(shí)間的增長(zhǎng),access.log和error.log文件會(huì)越來(lái)越大,本文主要介紹了分割nginx日志的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04詳解Nginx配置SSL證書(shū)實(shí)現(xiàn)Https訪問(wèn)
這篇文章主要介紹了詳解Nginx配置SSL證書(shū)實(shí)現(xiàn)Https訪問(wèn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Debian系統(tǒng)下為PHP程序配置Nginx服務(wù)器的基本教程
這篇文章主要介紹了Debian系統(tǒng)下為PHP程序配置Nginx服務(wù)器的基本教程,這里使用到了FastCGI和php-fpm,需要的朋友可以參考下2015-12-12Nginx實(shí)現(xiàn)負(fù)載均衡和反向代理的方法
Nginx是由俄羅斯人研發(fā)的,應(yīng)對(duì)Rambler的網(wǎng)站,并且2004年發(fā)布的第一個(gè)版本,Nginx功能豐富,可作為HTTP服務(wù)器,也可作為反向代理服務(wù)器,郵件服務(wù)器,本文給大家介紹了Nginx實(shí)現(xiàn)負(fù)載均衡和反向代理的方法,需要的朋友可以參考下2024-02-02