blob流数据转换成File文件或者下载

  • 转File文件
// data为blob文件流数据;fileName为文件名称,带后缀;type为文件类型,如'application/pdf'、'image/png'等
const filePDF = new File([data], fileName, { type })
  • 下载
/**
 * 下载blob文件流数据
 * @param {*} fileName 文件名称,带后缀
 * @param {*} data blob文件流
 */
downloadBlobData(fileName, data) {
  const uA = window.navigator.userAgent
  const isIE =
  /msie\s|trident\/|edge\//i.test(uA) &&
  !!(
    'uniqueID' in document ||
    'documentMode' in document ||
    'ActiveXObject' in window ||
    'MSInputMethodContext' in window
  )
  const url = window.URL.createObjectURL(new Blob([data]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  // 兼容IE
  if (isIE) {
    navigator.msSaveBlob(new Blob([data]), fileName)
  } else {
    link.click()
  }
},
posted @   jiazq  阅读(834)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示