OSS 文件下载-Excel

  • 发起请求网址如果是 www.baidu.com,跨域下载 Google cdn 的资源

Excel 文件

背景:

  • Excel 模板存储在 OSS 上,提供的一个链接,需要支持 用户点击下载

方案:

  • V1
    • 问题:跨域 a标签 download 属性失效
html 复制代码
<a href="https://cdn.google.com/1.xlsx" target="_blank" download="教师信息表模板.xlxs">教师信息表模板</a>
  • V2
    • 使用现有请求库,比如 axios,因为大部分请求库都是基于业务特性进行了封装,包含了 withCredentials:true

    • 在跨域下载的场景,需要配置 withCredentials:false,不携带 cookie

js 复制代码
requestLib.get('https://cdn.google.com/1.xlsx', { 
  withCredentials: false
})

或者 
requestLib.download('https://cdn.google.com/1.xlsx', {
  fileName: '教师信息表模板.xlxs',
  withCredentials: false
})
  • V3
js 复制代码
import { saveAs } from 'save-as'

const xhr = new XMLHttpRequest()
xhr.open('GET', 'https://cdn.google.com/1.xlsx', true)
xhr.responseType = 'blob'
xhr.onload = () => {
  if (xhr.status === 200) {
    saveAs(xhr.response, `${this.arrangeTypeTplName}.xlsx`)
  }
}
xhr.send()

V4:

js 复制代码
saveAs('https://cdn.google.com/1.xlsx',  '教师信息表模板.xlxs')
相关推荐
gCode Teacher 格码致知7 小时前
Python教学:字符编码的四种环境-由Deepseek产生
开发语言·python
铁链鞭策大师7 小时前
JavaEE之多线程
java·开发语言·java-ee
我是唐青枫7 小时前
Java Optional 实战指南:优雅处理空值与链式转换
java·开发语言
basketball6168 小时前
设计模式入门:2. 工厂模式详解 C++实现
开发语言·c++·设计模式
Lumbrologist8 小时前
【C++】零基础入门 · 第 16 节:智能指针
开发语言·c++
yu85939588 小时前
MATLAB 分支定界法(Branch and Bound)实现
开发语言·matlab
学会去珍惜8 小时前
c语言编程 C语言入门 c语言(C语言程序设计教程 c语言视频教程 c语言零基础
c语言·开发语言
吃阿茶搽8 小时前
源码剖析:Standard组件架构与底层实现原理
javascript
卤蛋fg68 小时前
给 vxe-table 设置全局默认参数:setConfig、setIcon 与 setTheme
vue.js