vue二进制文件下载

 第一种方法

        let parmas = {}
        introduction(parmas).then(res => {
        // console.log(res);
        const content = res.data;   //后端返回的数据
        let url = window.URL.createObjectURL(content)
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('id', 'downloadLink')
        var date =
          new Date().getFullYear() +
          "年" +
          (new Date().getMonth() + 1) +
          "月" +
          new Date().getDate();
        link.setAttribute('download', '应急厅最新题目-' + date + '.xlsx')
        document.body.appendChild(link)
        // 删除添加的a链接
        let objLink = document.getElementById('downloadLink')
        objLink.click();
        document.body.removeChild(objLink)
        // 释放内存
        window.URL.revokeOjbectURL(url)
        

第二种方法:

        const data = res.data
        const url = window.URL.createObjectURL(new Blob([data], { type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }))
        const a = document.createElement('a')
        a.href = url
        a.click()

你可能感兴趣的:(vue.js,javascript,前端)