Axios实现excel表格下载
Axios({ url: 'url', method: 'post', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, transformRequest: [function (data) {//请求发起前进行参数拼接(非必须,根据实际情况考虑) let ret = ''; for (let it in data) { if (it) { ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'; } } return ret; }], data: data, withCredentials: true, responseType: 'blob'// 返回的数据格式,必填,不然会返回乱码 }).then((resp) => { console.log(resp); let href = window.URL.createObjectURL(new Blob([resp.data])); let link = document.createElement('a'); let fileName = decodeURI(resp.headers['content-disposition'].split('filename =')[1]); link.style.display = 'none'; link.href = href; link.setAttribute('download', fileName); document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(href); });
本文来自博客园,作者:至善为止(如切如磋,如琢如磨),转载请注明原文链接:https://www.cnblogs.com/linzewei27/p/13743232.html