this.$http
    .post(url,{},{
		responseType: 'blob'
	})
	.then(res => {
		let content = res.data
		let blob = new Blob([content], {
		    type: "application/vnd.ms-excel;charset=utf-8"
	    });
        let elink = document.createElement('a')
        // 非IE下载
	    if ('download' in elink ) { 
	        elink.download = "XXX" // 文件名字
	        elink.style.display = 'none'
	        elink.href = URL.createObjectURL(blob)
	        document.body.appendChild(elink)
	        elink.click()

	        URL.revokeObjectURL(elink.href) 
	        document.body.removeChild(elink)
	    } 
        // IE10+下载
        else { 
		    navigator.msSaveBlob(blob, fileName)
	    }
	})
	.catch(err => {
			console.log(err);
	})

注意:不加响应类型会乱码

{
    responseType: 'blob'
}

 

Logo

前往低代码交流专区

更多推荐