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')
相关推荐
kkai人工智能2 小时前
AI写作:从“废话”到“爆款”
开发语言·人工智能·ai·ai写作
lizz315 小时前
C++模板编程:从入门到精通
java·开发语言·c++
shoubepatien6 小时前
JAVA -- 05
java·开发语言
寰天柚子6 小时前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
沐知全栈开发6 小时前
Bootstrap 下拉菜单:设计与实现指南
开发语言
晚烛6 小时前
实战前瞻:构建高可靠、强协同的 Flutter + OpenHarmony 智慧教育平台
javascript·flutter·html
Evand J6 小时前
【MATLAB例程】多锚点RSSI定位和基站选择方法,基于GDOP、基站距离等因素。以Wi-Fi定位为例,附下载链接
开发语言·matlab·定位·gdop·rssi
superman超哥6 小时前
仓颉语言中锁的实现机制深度剖析与并发实践
c语言·开发语言·c++·python·仓颉
JAVA+C语言7 小时前
String Constant Pool
java·开发语言
保护我方头发丶7 小时前
ESP-wifi-蓝牙
前端·javascript·数据库