Los datos binarios en Excel

atributo href es a través del principio de una etiqueta de datos de la tabla en forma binaria, y luego descargar el archivo a descargar atributo local;
Tenga en cuenta que el tipo de datos de la interfaz para ajustarse a la solicitud de blob tipo de referencia específica axios https: //www.npmjs .com / paquete / axios

  responseType: 'blob'

código específico

   let url = window.URL.createObjectURL(res.data);
   let link = document.createElement('a');
   link.style.display = 'none';
   link.href = url;
   link.setAttribute('download', filename); // filename 自定义下载的表格名称及后缀名;
   document.documentElement.appendChild(link);
   link.click();
   document.documentElement.removeChild(link);

Ejemplo completo:

	 // 首先要引入axios
	  import axios from 'axios';
	// 组件内部新增方法
      downloadExl(filename) {
        axios({
            method: 'get',
            url: '/api/export',
            params: Object, // Object 导出接口所需要的参数对象 
            responseType: 'blob'
          }).then(res => {
            let url = window.URL.createObjectURL(res.data);
            let link = document.createElement('a');
            link.style.display = 'none';
            link.href = url;
            link.setAttribute('download', filename);
            document.documentElement.appendChild(link);
            link.click();
            document.documentElement.removeChild(link);
          }).catch(e => {
          	console.log('导出失败')
          }).finally(() => {
          	console.log('不管导出成功/失败')
          });
      },
Publicado 58 artículos originales · ganado elogios 20 · vistas 110 000 +

Supongo que te gusta

Origin blog.csdn.net/fly_wugui/article/details/103383800
Recomendado
Clasificación