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')
相关推荐
Felven几秒前
C. Yet Another Card Deck
c语言·开发语言
一直都在572几秒前
SpringBoot+Vue+Netty+WebSocket+WebRTC 实现视频聊天
vue.js·spring boot·websocket
SuperEugene5 分钟前
数组的 10 个常用操作:map / filter / reduce 实战套路
前端·javascript
「QT(C++)开发工程师」6 分钟前
【Qt Creator 15.0.1 安装指南】
开发语言·qt
晓得迷路了6 分钟前
栗子前端技术周刊第 117 期 - TypeScript 6.0 Beta、webpack 2026 年路线图、React 最新生态调查报告结果...
前端·javascript·react.js
网小鱼的学习笔记8 分钟前
leetcode283移动零元素
java·开发语言·算法
一点多余.10 分钟前
java中的单例模式
java·开发语言·单例模式
lzhdim12 分钟前
CSS实现毛玻璃模糊效果
前端·css
xyq202412 分钟前
NumPy 创建数组
开发语言
lly20240615 分钟前
Ruby 类和对象
开发语言