JavaScript流文件下载实现详解

文章的更新路线:JavaScript基础知识-Vue2基础知识-Vue3基础知识-TypeScript基础知识-网络基础知识-浏览器基础知识-项目优化知识-项目实战经验-前端温习题(HTML基础知识和CSS基础知识已经更新完毕)

正文

前端web、h5实现方式

if (!res) return false
const blob = new Blob([res.data], { type'application/vnd.ms-excel' })
if (window.navigator.msSaveOrOpenBlob) {
  navigator.msSaveBlob(blob, '数据分析.xlsx')
else {
  const link = document.createElement("a")
  link.href = window.URL.createObjectURL(blob)
  link.download = new Date().getTime()

  // fix Firefox
  const body = document.querySelector("body")
  link.style.display = "none"
  body.appendChild(link)

  link.click()
  body.removeChild(link)
  window.URL.revokeObjectURL(link.href)
}

小程序实现方式

// videoContent流文件数据
const data = videoContent.result

// 创建ArrayBuffer对象
let ab = new ArrayBuffer(data.data.data.length)

// 创建TypedArray对象并装载ArrayBuffer
let bufferView = new Uint8Array(ab)

// 遍历接收ArrayBuffer对象中的原始数据
// for (let i = 0; i < data.data.data.length; ++i) {
//   bufferView[i] = data.data.data[i]
// }

// 同上面for循环实现的结果
bufferView.set(data.data.data)

// 获取全局文件管理对象,微信小程序使用wx.getFileSystemManager()
const fs = uni.getFileSystemManager()

// 声明本地缓存地址
const sourceURI = uni.env.USER_DATA_PATH + '/' + new Date().getTime() + '.mp4'

// 将内容写入本地地址内存中
let that = this
fs.writeFile({
  filePath: sourceURI,
  data: bufferView.buffer,
  encoding: 'utf8',
  success: function(res) {
    that.realUrl = sourceURI
  },
  fail: function(error) {
    console.log(error)
  }
})

MIME TYPE

文件后缀 MIME TYPE
.doc application/msword
.dot application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.document.macroEnabled.12
.dotm application/vnd.ms-word.template.macroEnabled.12
.xls application/vnd.ms-excel
.xlt application/vnd.ms-excel
.xla application/vnd.ms-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm application/vnd.ms-excel.sheet.macroEnabled.12
.xltm application/vnd.ms-excel.template.macroEnabled.12
.xlam application/vnd.ms-excel.addin.macroEnabled.12
.xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt application/vnd.ms-powerpoint
.pot application/vnd.ms-powerpoint
.pps application/vnd.ms-powerpoint
.ppa application/vnd.ms-powerpoint
.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12
.zip application/zip
.tar application/x-tar

结束语

今天分享,有需要的自行获取(回复 11)。

alt

本文由 mdnice 多平台发布

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值