js javascript 下载url为文件 下载项目里的文件

下载项目里的文件

 

// F:\projectCode\xxx-client\src\excel\pointTmt.xlsx

import eventTemplate from '@/excel/pointTmt.xlsx'

<a download="埋点文档格式规范.xlsx" :href="eventTemplate"> 埋点文档格式规范 </a>

 

 

 

 

下载url为文件

// const URL = window.URL || window.webkitURL || window.moxURL

当参数不是blob时需要生成blob

https://www.cnblogs.com/dhjy123/p/14988142.html

// const blob = new Blob([res])

 

function openDownloadDialog (url, saveName) {
    if (typeof url === 'object' && url instanceof Blob) {
      url = URL.createObjectURL(url)
    }
    var aLink = document.createElement('a')
    aLink.href = url
    aLink.download = saveName || ''
    var event
    if (window.MouseEvent) event = new MouseEvent('click')
    else {
      event = document.createEvent('MouseEvents')
      event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    }
    aLink.dispatchEvent(event)
  }

  openDownloadDialog(sheet2blob(sheet), xmlName + '.xlsx')

 2、

const retData = await this.$api.userGroup.getOssFile(data.url)
  this.handleSaveAs(retData, record.title, record.id)


    handleSaveAs(blob, filename, id) {
      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)
      }
    }

 

posted @ 2021-10-27 15:36  hjswlqd  阅读(412)  评论(0编辑  收藏  举报