vue 文件流下载文件

155 阅读1分钟

请求方法必须 config.responseType = 'arraybuffer';

// 下载
    download (orderId) {
      const vm = this;
      this.$axios.get('/pay/getOrderByOrderId', { params: { orderId: orderId } }).then((data) => {
        if (data.code == 500 || data.code == 401) {
          // util.removeSession('userInfo');
          util.setSession('page', '/lt');
          util.removeCookie('__USER_INFO');
          this.$router.push({ path: '/overTime', replace: true });
        }
        const url = window.URL.createObjectURL(new Blob([data]));
        const link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute(
          'download',
          '招标数据' + vm.Format('yyyy-MM-dd hh:mm:ss', new Date()) + '.xls'
        );
        document.body.appendChild(link);
        link.click();
        // vm.downwaiting = true;
        vm.$message.success('下载完成');
      });
    },
    Format (fmt, datatime) {
      // author: meizz
      var o = {
        'M+': datatime.getMonth() + 1, // 月份
        'd+': datatime.getDate(), // 日
        'h+': datatime.getHours(), // 小时
        'm+': datatime.getMinutes(), // 分
        's+': datatime.getSeconds(), // 秒
        'q+': Math.floor((datatime.getMonth() + 3) / 3) // 季度
      };
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (datatime.getFullYear() + '').substr(4 - RegExp.$1.length)
        );
      }
      for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length == 1
              ? o[k]
              : ('00' + o[k]).substr(('' + o[k]).length)
          );
        }
      }
      return fmt;
    },