Vue combined import and export Excel questions Detailed background

Man of few words said, directly on the front-end code

Axios ({ 
  Method: ' POST ' , 
  URL: ' HTTP: // localhost: 19090 / exportUser ' , // this is a request for the address of 
  the params : { // Parameters of this request is 
   In Email: the this .email, 
   startRegisterDate: the this . registerStartTime, 
   endRegisterDate: the this .registerEndTime 
  }, 
  responseType: ' BLOB ' 
  .}) the then ((RES) => { 
  the console.log (RES) 
  const Link = document.createElement ( ' A '  )
  the let BLOB= new Blob([res.data],{type: 'application/vnd.ms-excel'});
  link.style.display = 'none'
  link.href = URL.createObjectURL(blob);
  let num = ''
  for(let i=0;i < 10;i++){
   num += Math.ceil(Math.random() * 10)
  }
  link.setAttribute('download', '用户_' + num + '.xls')
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  }).catch(error => {
 
  console.log(error)
  })

A closer look axios request added a responseType: 'blob'configuration, it is very important

I request the head of the project because there need to add sessionId, it is necessary to use the above method

 

If you get a request, the request header does not require extra parameters directly window.location.href = 'http: // localhost: 19090 / exportUser email =?' + Email + "& start =" + start, you can open an address

'

 

Guess you like

Origin www.cnblogs.com/ttjm/p/11307462.html