ios的safari下载文件 文件名乱码

当使用nginx代理文件并下载文件时,返回的协议头Content-Disposition中filename=%E9%9B%AA%E5%B1%B1.jpg中文内容会是URL编码的形式,当客户端在safari浏览器下载下载文件时,文件名不会转换(URL解码)为正常的中文。

应该使用js进行下载,即可解决问题。

js 复制代码
       axios({
          method: 'get',
          url: 'http://xxxxxxxx.com/files/年终总结模板.docx',
          responseType: 'blob'
        }).then((response) => {
          const contentDisposition = response.headers['content-disposition'];
          let fileName = ''; // 如果没获取到文件名,设置默认文件名
          if (contentDisposition) {
            const matches = contentDisposition.match(/filename="?([^"]+)"?/);
            if (matches && matches.length > 1) {
              fileName = matches[1];
            }
          }
          const blob = new Blob([response.data], { type: response.headers['content-type'] });
          const link = document.createElement('a');
          link.href = URL.createObjectURL(blob);
          link.download = decodeURIComponent(fileName);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          URL.recycleObjectURL(link.href);
        })
相关推荐
陈随易14 分钟前
Rolldown 把插件改动回传提速 26 倍
前端·后端·程序员
笃行3508 小时前
集中式与分布式,KingbaseES 的四种形态共用一套内核
前端
陈随易9 小时前
Vue 编译器换掉 source map,快了三四成
前端·后端·程序员
ZhengEnCi10 小时前
12 种高级 UI 交互效果及对应 AI 提示词:从按压回弹到底部抽屉的物理感教学
前端·css·交互设计
IT_陈寒13 小时前
Redis大key删除为什么会导致服务卡顿?我的线上事故复盘
前端·人工智能·后端
用户27050777252414 小时前
MCP 最小 Server 实测:3 个 JSON-RPC 报错与修法
前端
用户4993350250714 小时前
一人VIP账号多人共用:Web 代理里的会话共享怎么实现
前端
用户老李14 小时前
无线电自学教程 05:第一台收音机——从矿石机到 SDR
前端
IPIPD14 小时前
Node.js请求遇到503怎么重试?先解析Retry-After,再做有上限退避
前端
葡萄城技术团队14 小时前
dsh-spreadjs-editor 的集成链路:把 SpreadJS Designer 嵌进 Agent 工作台
前端