美文网首页
vue+axios excle文件导出

vue+axios excle文件导出

作者: 妖刀_ | 来源:发表于2019-09-30 17:41 被阅读0次

    res 为接口返回数据   当获取excle文件时  接口请求头配置

    axios.responseType ='blob'

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    const blob =new Blob([res.data])          // 将返回二进制数据  创建Blob文件

    const fileName =`discount-code-${Date.parse(new Date())}.xls`  // 导出文件名 + 时间毫秒值 避免名称重复

    if ('download' in document.createElement('a')) {

    const elink = document.createElement('a')  创建 a标签

    elink.download = fileName  //  创建文件名

    elink.style.display ='none'  //   导出文件样式

      elink.href = URL.createObjectURL(blob)  创建a 标签 href 

    document.body.appendChild(elink)  

    elink.click()

    URL.revokeObjectURL(elink.href)  

    document.body.removeChild(elink)  释放流文件

    }

    相关文章

      网友评论

          本文标题:vue+axios excle文件导出

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