1.post way in to add responseType: 'blob' parameters, or download the excel will be messy
2. Use {type: "application / vnd.ms-excel"} written, can be saved as excel file xls format (compatible with older versions). The use of "application / vnd.openxmlformats-officedocument.spreadsheetml.sheet" will be saved as xlsx
3. Return the result is a link to download excel document using window.open (result) to
4. Add Node call click method, without using window.open (objectUrl) method, is prevented by the browser when the plug connector block pop
The name is set to the file, attribute settings directly download a tag to
1, Method 1
axios.post('/getExcel',{},{responseType:'blob'}).then((res:any)=>{ let reader = new FileReader() reader.readAsDataURL(res); reader.onload = (e:any)=>{ var a = document.createElement('a'); document.body.appendChild(a); a.style.display = 'none'; a.href = e.target.result; a.download = 'name.xlsx'; a.click(); a.remove(); } })
2, Method 2
var download = function (file_name:string, content:any) { var csvData = new Blob([content], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }); // for IE if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(csvData, file_name); } // for Non-IE (chrome, firefox etc.) else { var a = document.createElement('a'); document.body.appendChild(a); a.style.display = 'none'; var url = window.URL.createObjectURL(csvData); a.href = url; a.download = file_name; a.click(); a.remove(); window.URL.revokeObjectURL(url); } }; this.$axios.post('/getExcel',{},{responseType:'blob'}).then((res:any)=>{ download('name',res); })