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')
相关推荐
htzyl2063 分钟前
javaScript——this 的四种绑定:为什么箭头函数改不了它
前端·javascript
bai27314 分钟前
*.js 匹配到了 B.JS:fs.globSync 在 Windows 上的五个默认值
javascript·后端
geovindu15 分钟前
python: Decision Tree
开发语言·后端·python·决策树
时间的情敌1 小时前
Three.js 工业场景性能优化合集|大批量管线、InstancedMesh 实例化、合并 Geometry、显存释放
开发语言·javascript·性能优化·three.js
Sakura_j_h_2 小时前
TypeScript 7.0 换 Go 编译器后,我先在 6.0 清完默认值
javascript·golang·typescript·vite·前端工程化·tsc
用户298698530142 小时前
在 React 中实现 Excel 与 TXT 格式互转的前端方案
javascript·react.js·excel
步峰代理-盾山2 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
map_3d_vis2 小时前
百度地图地图类型切换:普通地图、卫星图、混合图
前端·javascript·地图·百度地图
BruceVan3 小时前
给 AI 配上 Context7:查文档更准,还更省 token
vue.js·spring boot·ai编程
sharemylove12 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习