美文网首页
vue+element-ui 识别java文件流 下载或预览

vue+element-ui 识别java文件流 下载或预览

作者: 把我推进深海的你真善良 | 来源:发表于2020-11-03 20:03 被阅读0次

    下载/预览

        getExcel() {
          fetchExcel().then(response => {
            console.log(response)
            console.log(new Blob([response]))
            // 下载
            // const aLink = document.createElement('a')
            // const blob = new Blob([response], { type: 'application/vnd.ms-excel' })
            // console.log(blob)
            // aLink.href = URL.createObjectURL(blob)
            // aLink.setAttribute('download', '哈哈哈哈' + '.xlsx') // 设置下载文件名称
            // aLink.click()
    
          // 返回数据 html 或者json
            const fileReader = new FileReader()
            fileReader.onload = ev => {
              try {
                const data = ev.target.result
                const workbook = XLSX.read(data, { type: 'binary' })
                console.log(workbook)
                const firstSheetName = workbook.SheetNames[0]
               const worksheet = workbook.Sheets[firstSheetName]
                const header = this.getHeaderRow(worksheet)
                const results = XLSX.utils.sheet_to_html(worksheet)
              // const results = XLSX.utils.sheet_to_json(worksheet)
                this.$emit('tablelist', data)
              } catch (e) {
                return false
              }
            }
            fileReader.readAsBinaryString(response)
          })
        }
    

    请求

    export function fetchExcel(id) {
      return request({
        url: '/tbTemplate/v1/downloadTemplateFile/7',
        method: 'post',
        responseType: 'blob' // 重要
      })
    }
    
    

    相关文章

      网友评论

          本文标题:vue+element-ui 识别java文件流 下载或预览

          本文链接:https://www.haomeiwen.com/subject/jhpivktx.html