axios下载excel(后台返回文件流)

693 阅读1分钟
//url:api地址
//params:参数
axios.get(url,  {
	params:params,
    responseType: 'blob'
}).then(res = >{
    let blob = new Blob([res], {
        type: "application/vnd.ms-excel"
    });
    let objectUrl = URL.createObjectURL(blob);

    let a = document.createElement("a");
    a.href = objectUrl;
    a.download = "客户";
    //a.click();
    //下面这个写法兼容火狐
    a.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}));
    window.URL.revokeObjectURL(blob);
})