项目中导出功能(word)

导出方法:

fileName:导出word文件名称
this.url.exportword:接口地址,返回blob文件流
exportsMethod(){
      let fileName = this.info.lcmc
      getActionBlob(this.url.exportword, {id:this.id}).then((data) => {
        if (!data) {
          this.$message.warning("文件下载失败")
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data],{type: 'application/msword'}), fileName+'.doc')
        }else{
          let url = window.URL.createObjectURL(new Blob([data],{type: 'application/msword'}))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName+'.doc')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      })
    }
getActionBlob:
export function getActionBlob(url, parameter) {
    return axios({
        url: url,
        method: 'get',
        params: parameter,
        responseType: 'blob'
    })
}
其他文件类型:
fileTypes : []

判断文件类型
initFileTypes(){
        let map = new Map()
        map.set('txt','text/plain')
        map.set('xlsx','application/vnd.ms-excel')
        map.set('xls','application/vnd.ms-excel')
        map.set('doc','application/msword')
        map.set('docx','application/vnd.openxmlformats-officedocument.wordprocessingml.document')
        map.set('pdf','application/pdf')
        map.set('zip','application/zip')
        map.set('rar','application/zip')
        map.set('png','image/png')
        map.set('jpg','image/jpeg')
        map.set('jpeg','image/jpeg')
        map.set('ppt','application/vnd.ms-powerpoint')
        map.set('pptx','application/vnd.ms-powerpoint')
        this.fileTypes = map
      },

下载方法:
download(record){
        let fileType = record.wjmc.split('.')
        fileType = fileType[fileType.length - 1]
        fileType = this.fileTypes.get(fileType)
        if(!fileType){
          this.$message.info('暂不支持该类型文件下载')
        }

        let address = this.url.liu + 'flyz' + this.ajId+ '/fj'+ record.wjmc;
        getActionBlob(address).then(res => {
          var blob = new Blob([res], {type: fileType + ';charset=utf-8'}); //type这里表示xlsx类型
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = record.wjmc; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象

        })
      },

 

 
posted @ 2022-10-14 16:33  阖彩  阅读(82)  评论(0编辑  收藏  举报