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')
相关推荐
心平气和量大福大4 分钟前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
码哥DFS18 分钟前
构造函数、实例对象、对象原型 ----三者关系
开发语言·javascript·原型模式
满栀58538 分钟前
vue动态路由效果
前端·javascript·vue.js·前端框架·vue
quantdash_cc1 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
ydd1001001 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
半亩码田1 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
心运软件2 小时前
SpringBoot+ Vue校园社团管理平台的完整架构设计
vue.js·后端
Wzx1980122 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
jjw_zyfx2 小时前
css vue vite实现闪烁的呼吸效果
javascript·css·vue.js
牧羊人.3332 小时前
Python 办公自动化从入门到入土|09 数据容器之字典
开发语言·python