How to download gzip file using AngularJS from REST API response? It works with REST API

270 Views Asked by At

Here is my problem - The response data is a binary representation of a tar.gz file.

These data are generated from the following Java code (A REST Endpoint logic ) from the server:

  public ResponseEntity<InputStreamResource> downloadLogs(String nodeName, String storagePoolName){
    ResponseEntity<InputStreamResource> responseEntity = null;

    File initialFile2 = new File("C:\\Users\\Admin\\Downloads\\logs\\pz_logs_temp.tar.gz");
    InputStream targetStream = FileUtils.openInputStream(initialFile2);

    //Preparing Response
    InputStreamResource resource = new InputStreamResource(targetStream);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment");
    responseHeaders.add("filename", String.format("%s.tar.gz",nodeName));
    responseEntity = ResponseEntity.ok()
         .headers(responseHeaders)
         .contentType(MediaType.APPLICATION_OCTET_STREAM)
         .body(resource);
    return responseEntity;
   }

This is the picture on the Java Script side:

enter image description here

This is how the data looks in console:

enter image description here

This is a response headers

enter image description here

The code generates a tar.gz file and downloads it. However, the generated file is not a valid tar.gz file. I could not open it.

I think I am missing something on the Java Script side.

0

There are 0 best solutions below