当前位置: 首页 > Web前端 > HTML5

vuexml文档流下载文件

时间:2023-04-05 18:54:00 HTML5

exportdefault{方法:{download(url,param){letparams=paramletxhr=newXMLHttpRequest();xhr.open('POST',url,true);xhr.setRequestHeader("Content-Type",'application/json;charset=utf-8');xhr.responseType='blob';xhr.onload=function(e){if(this.status==200){if(navigator.msSaveBlob){returnnavigator.msSaveBlob(this.response,params.originalName?params.originalName:'errordocument.xlsx')}让blob=this.response;让a=document.createElement('a');让url=window.URL.createObjectURL(blob);a.href=网址;//获取后端文件名letfileName=decodeURI(xhr.getResponseHeader('content-disposition'));//截取=字符串后面的内容letstr=fileName.substring(21,fileName.length)letutfStr=decodeURI(escape(str))//ISO_8859_1格式->改为utf-8a.download=utfStr;a.点击();window.URL.revokeObjectURL(url);}};//参数为json格式xhr.send(JSON.stringify(params));}}}