OSS 文件下载-Excel

  • 发起请求网址如果是 www.baidu.com,跨域下载 Google cdn 的资源

Excel 文件

背景:

  • Excel 模板存储在 OSS 上,提供的一个链接,需要支持 用户点击下载

方案:

  • V1
    • 问题:跨域 a标签 download 属性失效
<a href="https://cdn.google.com/1.xlsx" target="_blank" download="教师信息表模板.xlxs">教师信息表模板</a>
  • V2
    • 使用现有请求库,比如 axios,因为大部分请求库都是基于业务特性进行了封装,包含了 withCredentials:true

    • 在跨域下载的场景,需要配置 withCredentials:false,不携带 cookie

requestLib.get('https://cdn.google.com/1.xlsx', { 
  withCredentials: false
})

或者 
requestLib.download('https://cdn.google.com/1.xlsx', {
  fileName: '教师信息表模板.xlxs',
  withCredentials: false
})
  • V3
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:

saveAs('https://cdn.google.com/1.xlsx',  '教师信息表模板.xlxs')

相关推荐

  1. OSS 文件下载-Excel

    2024-04-29 08:14:01       13 阅读
  2. ajax 下载文件excel导出)

    2024-04-29 08:14:01       41 阅读
  3. 【Golang】实现 Excel 文件下载功能

    2024-04-29 08:14:01       12 阅读
  4. 前端下载导出文件流,excel/word/pdf/zip等

    2024-04-29 08:14:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-29 08:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-29 08:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-29 08:14:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-29 08:14:01       20 阅读

热门阅读

  1. 【Firewall】服务器访问限制白名单

    2024-04-29 08:14:01       11 阅读
  2. 无人机在测绘领域的应用

    2024-04-29 08:14:01       12 阅读
  3. 41 对MVC,MVP,MVVM的理解

    2024-04-29 08:14:01       10 阅读
  4. 2385. 感染二叉树需要的总时间

    2024-04-29 08:14:01       13 阅读
  5. Spark RDD

    Spark RDD

    2024-04-29 08:14:01      9 阅读
  6. 从零学算法135

    2024-04-29 08:14:01       9 阅读
  7. Python.第六章函数应用实例

    2024-04-29 08:14:01       11 阅读
  8. Chrome插件开发:开启浏览器功能的无限可能

    2024-04-29 08:14:01       14 阅读
  9. Rapidly exploring Random Trees(RRT)类算法

    2024-04-29 08:14:01       13 阅读