• 前端下载文件重命名


    //引入使用
     downloadFileRename(url,name.ext)

    //下载文件并重命名
    export function downloadFileRename(url, filename) {
      function getBlob(url) {
        return new Promise((resolve) => {
          const xhr = new XMLHttpRequest()
          xhr.open('GET', url, true)
          xhr.responseType = 'blob'
          xhr.onload = () => {
            if (xhr.status === 200) {
              resolve(xhr.response)
            }
          }
          xhr.send()
        })
      }
      function saveAs(blob, filename) {
        if (window.navigator.msSaveOrOpenBlob) {
          navigator.msSaveBlob(blob, filename)
        } else {
          const link = document.createElement('a')
          const body  = document.querySelector('body')

          link.href = window.URL.createObjectURL(blob)
          link.download = filename

          // fix Firefox
          link.style.display = 'none'
          body.appendChild(link)

          link.click()
          body.removeChild(link)

          window.URL.revokeObjectURL(link.href)
          return
        }
      }
      getBlob(url).then((blob) => {
        saveAs(blob, filename)
      })
    }

     

    1. //引入使用
    2. downloadFileRename(url,name.ext)
    3. //下载文件并重命名
    4. export function downloadFileRename(url, filename) {
    5. function getBlob(url) {
    6. return new Promise((resolve) => {
    7. const xhr = new XMLHttpRequest()
    8. xhr.open('GET', url, true)
    9. xhr.responseType = 'blob'
    10. xhr.onload = () => {
    11. if (xhr.status === 200) {
    12. resolve(xhr.response)
    13. }
    14. }
    15. xhr.send()
    16. })
    17. }
    18. function saveAs(blob, filename) {
    19. if (window.navigator.msSaveOrOpenBlob) {
    20. navigator.msSaveBlob(blob, filename)
    21. } else {
    22. const link = document.createElement('a')
    23. const body = document.querySelector('body')
    24. link.href = window.URL.createObjectURL(blob)
    25. link.download = filename
    26. // fix Firefox
    27. link.style.display = 'none'
    28. body.appendChild(link)
    29. link.click()
    30. body.removeChild(link)
    31. window.URL.revokeObjectURL(link.href)
    32. return
    33. }
    34. }
    35. getBlob(url).then((blob) => {
    36. saveAs(blob, filename)
    37. })
    38. }
  • 相关阅读:
    五子棋(C语言实现)
    Java 传统的生产者与消费问题以及虚假唤醒
    Go语言中list列表的基本操作(插入删除遍历以及实现栈与队列)
    Spring Boot 跨域解决方案
    DAP数据调度功能完善说明
    “ApproachingTheTarget“ app Tech Support(URL)“ app Tech Support(URL)
    linux命令使用
    机器学习:银行贷款违约预测模型
    基于JAVA的图书借阅管理平台【数据库设计、源码、开题报告】
    实验五 函数文件(matlab)
  • 原文地址:https://blog.csdn.net/u014645827/article/details/134420179