Home > Software design >  Open PDF in new tab in Angular
Open PDF in new tab in Angular

Time:10-15

I am downloading the file with angular8 and spring boot. I want to open the PDF files I saved in a new tab can youhelp me instead of downloading them ?

 FileDownload(fileId: number, headers): Observable<any> {
    return this.http.get(apiHost   '/downloadFile/'   fileId, { headers, responseType: 'blob' as 'json' });

  }
  
  downloadFile(event): void {
    const headers = new HttpHeaders();

    this.service.FileDownload(event.fileId, headers).subscribe(response => {

      let dataType = response.type;
      let binaryData = [];
      binaryData.push(response);

      let downloadLink = document.createElement('a');
      downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));
      if (event.fileName)
        downloadLink.setAttribute('download', event.fileName);
      document.body.appendChild(downloadLink);
      downloadLink.click();

    })


  }

UPDATE(Working Code)

 this.service.FileDownload(event.fileId, headers).subscribe(response => {
      let dataType = response.type;
      let binaryData = [];
      binaryData.push(response);

      const fileURL = URL.createObjectURL(new Blob(binaryData, { type: dataType }));
      window.open(fileURL, '_blank');


    })


Spring Booot (inline)

response.setHeader("Content-Disposition", String.format("inline; filename=\"%s\"", fileName));

CodePudding user response:

window.open(downloadLink.href, '_blank')

  • Related