title: How to open a preview file (pdf, docx, txt, etc.) in another page of the browser using Angular and Java
body: <p>I am developing a web application using Angular 7 and Java 8. I am uploading a file (pdf, docx, txt etc...), but the problem is that I can't open it in another page of the browser through a RESTful web service. I am getting the error 415 Unsupported Media Type. I have tried with the POST and GET method  whitout any success. These are the snippets of the code, front-end and back-end:</p>
  
  <p>Angular component (method called by a button passing the path + filename example : C/doc/foo.pdf)</p>
  
  <pre><code>download(doc) {     
       this.service.downloadFile(doc.path).subscribe(response =&gt; {
          if(response) {
             let blob = new Blob([response], { type: 'text/json; charset=utf-8' });
             const url= window.URL.createObjectURL(blob);
             window.open(url);
          }   
       });
   }
  </code></pre>
  
  <p>Angular service</p>
  
  <pre><code>downloadFile(path): Observable&lt;Blob&gt; {
      const url = '/download';
      return this.http.post&lt;Blob&gt;(url, path, { responseType: 'blob' as 'json' });
  }
  </code></pre>
  
  <p>Java Controller</p>
  
  <pre><code>@PostMapping(value = "download", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
  @ResponseBody
  public byte[] download(@RequestBody String path) {
      try {
          return this.provvedimentoService.getDownload(path);
      } catch (IOException | EgesException e) {
          e.printStackTrace();
          return null;
      }
  }
  </code></pre>
  
  <p>Java Service</p>
  
  <pre><code>public byte[] getDownload(String pathFile) throws EgesException, IOException {
      Path path = Paths.get(pathFile);
      if(path.toFile().exists())
          return Files.readAllBytes(path);
      else
          throw new EgesException(EgesExceptionConstants.WARNING_ACT_NOTFOUND_EXCEPTION);
  }
  </code></pre>