亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Angular 6 和 Spring Rest API 下載文件

使用 Angular 6 和 Spring Rest API 下載文件

aluckdog 2022-01-19 12:59:51
我在使用 angular 6 從 rest api 下載文件時遇到問題后端方法  @RequestMapping(value = "/print/{id}")    public ResponseEntity<byte[]> generateReport(@PathVariable("id") long id_project){        Map<String, Object> mapper = new HashMap<String, Object>();        byte[] content =  exportService.export(mapper, ReportUtils.testReport, ReportUtils.FileFormat.PDF.toString());        return new ResponseEntity<>(content, Utils.getPDFHeaders("Situation_test.pdf"), HttpStatus.OK);    }方法 getHeaderpublic static HttpHeaders getPDFHeaders(String fileName) {    HttpHeaders head = new HttpHeaders();    head.setContentType(MediaType.parseMediaType("application/pdf"));    head.add("content-disposition", "attachment; filename=" + fileName);    head.setContentDispositionFormData(fileName, fileName);    head.setCacheControl("must-revalidate, post-check=0, pre-check=0");    return head;}我的角度服務download(url: string): any {    let headers = new HttpHeaders();    headers = headers.append('Authorization', 'Bearer ' + this.getToken());    this.http.get(this.API_URL + url, {headers: headers}).subscribe((res) => {      const file = new Blob([res], {        type: 'application/pdf',      });      const a = document.createElement('a');      a.href = this.API_URL + (<any>res)._body;      a.target = '_blank';      document.body.appendChild(a);      a.click();      return res;    }, error => {      let alert: any = {        title: 'Notify Title',        body: 'Notify Body',      };      alert.body = error.error.message || JSON.stringify(error.error);      alert.title = error.error.error;      alert = this.alertService.handleError(error);      alert.position = 'rightTop';      console.log(error);      this.alertService.notifyError(alert);      return error;    });  }我已經使用 PostMan 嘗試過我的 API,它的詞很完美,但是在 Angular 中它給了我這個錯誤
查看完整描述

1 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

嘗試將內容類型添加到您的請求標頭。你可以試試這個作為一個例子:

let headers = new Headers({'Content-Type': 'application/pdf', 'Accept': 'application/pdf'});


查看完整回答
反對 回復 2022-01-19
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號