用nginx解决 前端js下载跨域问题

用nginx解决 前端js下载跨域问题

背景

订单系统增加附件 预览 , 下载的功能,但是这个附件是客户推单时推送过来的,文件连接是类似oss连接,但是是客户的域名,所以导致跨域问题.

前端是vue项目

解决过程

1.面向百度编程

先百度了一番

基本都是说用 blob 这个我真不太了解,但是在项目里找到了别人写的下载方法,也是用的blob,但是根本解决不了跨域问题.

于是我找了几个字多的,试了一下,根本没用

2.摆烂

百度一番没找到解决办法,于是打算摆烂

附件预览界面大约长这样子,直接鼠标在链接上面右键另存为可以下载附件

于是改造了一下原来的下载方法 增加个友好提示

javascript 复制代码
    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);
        }
      };
      // 增加友好提示 下载失败让用户右键另存为
      xhr.onerror = function (){
        _this.$modal.msgError("下载失败,请尝试在链接上使用右键另存为");
      }
      xhr.send();
    }

3.重新出发

增加友好提示以后,同事都要被我笑死了,这种方式简直low爆了

于是我想到用nginx反向代理来解决这个问题

继续百度

在csdn上找到一篇 地址

用本地nginx试了一下

shell 复制代码
location /download {
      if ($query_string ~*  ^(.*)url=(.*)$){
            set $imageUrl $2;
            proxy_pass $imageUrl;
      }
}

修改下载方法

javascript 复制代码
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("直接下载失败,请尝试在链接上使用右键另存为");
      }
      xhr.send();
}

居然成功了,可以下载文件, 于是修改了一下 上测试服务器

最终版js

javascript 复制代码
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("直接下载失败,请尝试在链接上使用右键另存为");
      }
      xhr.send();
}

4.新问题

上测试服务器后发现按下载按钮还是失败,发现居然报502

翻了一下nginx的报错日志

shell 复制代码
no resolver defined to resolve ........

百度了一下

原来nginx代理的地址如果是域名的话 需要增加dns解析 nginx并不会直接用系统的dns

shell 复制代码
location /download {
        resolver 你的dns;
        if ($query_string ~*  ^(.*)url=(.*)$){
                set $imageUrl $2;
                proxy_pass $imageUrl;
        }
}

设置完 resolver 后 再试 果然成功了

相关推荐
炫饭第一名8 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
Forever7_9 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
不会敲代码19 小时前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Angelial9 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
进击的尘埃10 小时前
Vue3 响应式原理:从 Proxy 到依赖收集,手撸一个迷你 reactivity
javascript
willow10 小时前
JavaScript数据类型整理1
javascript
LeeYaMaster10 小时前
20个例子掌握RxJS——第十一章实现 WebSocket 消息节流
javascript·angular.js
UIUV11 小时前
RAG技术学习笔记(含实操解析)
javascript·langchain·llm
SuperEugene11 小时前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
阿懂在掘金12 小时前
defineModel 是进步还是边界陷阱?双数据源组件的选择逻辑
vue.js·源码阅读