前端post下載實現


```javascript
exportService(url,params){
   url = this.budService.getBaseUrl()+url;
   return this.http.post(url,params,{responseType:'arraybuffer',observe:'response'}).toPromise().then(res=>{
   const blob=new Blob([res.body],{type:'application/vnd.ms-excel'});
   if(res.headers.get('Content-disposition')){
      const fileName=decodeURI(res.headers.get('Content-disposition').split('filename=')[1]);
      if(window.navigator.msSaveOrOpenBlob){
         navigator.msSaveBlob(blob,fileName)
      }else{
          const link = document.createElement('a');
	      link.setAttribute('href',window.URL.createObjectURL(blob));
	      link.setAttribute('download',fileName);
	      link.style.visibility='hidden';
	      document.body.appendChild(link);
	      link.click();
	      document.body.removeChild(link);
      }
    } else {
        var encodedString = utf8ArrayToStr(new Uint8Array(res.body));
        var obj = JSON.parse(encodedString);
        if(obj.code==='SYS-9000'){
          /**拒絕訪問  停留3秒*/
          this.message.error(`後臺提示:${obj.message}`,{gmDuration:3000});
        }else{
          this.modalService.error({
            title:'後臺錯誤提示',
            content:`${obj.message}`
          })
        }
    }
 })
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章