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')
相关推荐
程序喵大人1 分钟前
【C++进阶】STL算法与函数对象 - 04 find、count和any_of把查询写成意图
开发语言·c++·算法
Livia要学习9 分钟前
Python装饰器
开发语言·python
一直都在57212 分钟前
LangChain4j精讲
开发语言·人工智能
吹什么轩26 分钟前
c++复习:c++11:lambda表达式
开发语言·c++
不会代码的小猴41 分钟前
标准模板库(STL)
开发语言·c++·笔记·算法
雾时之林2 小时前
python--字符串
开发语言·python
啊哈一半醒2 小时前
Go 语言 Context 全方位详解:原理、实战与避坑
开发语言·后端·golang
铅笔侠_小龙虾2 小时前
rust 学习(9):结构体
开发语言·学习·rust
此生决int2 小时前
深入理解C++系列(11)——stack、queue和priority_queue
开发语言·c++