Vue2 Export Excel + solve the garbage problem - axios (download backstage pass over the stream file (excel) after the garbage problem)

  接口要求: post方法、入参为json格式、出参文件流
  axios:设置返回数据格式为 blob 或者 arraybuffer   ( 注意 )   

  axios({ // 用axios发送post请求
     method: 'post',
      url: ' /serviceTime/exportData', // 请求地址
      data: form, // 参数
      responseType: 'blob', // 表明返回服务器返回的数据类型
      headers: {
     'Content-Type': 'application/json'
     }
   }).then(res => { // 处理返回的文件流
     const blob = new Blob([res.data]);//new Blob([res])中不加data就会返回下图中[objece objece]内容(少取一层)
     const fileName = '统计.xlsx';//下载文件名称
     const elink = document.createElement('a');
     elink.download = fileName;
     elink.style.display = 'none';
     elink.href = URL.createObjectURL(blob);
     document.body.appendChild(elink);
     elink.click();
     URL.revokeObjectURL(elink.href); // 释放URL 对象
     document.body.removeChild(elink);
 })

Guess you like

Origin www.cnblogs.com/linyuxuan/p/12324136.html