1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Excel file generated in Springboot app which is not opening after downloaded from AWS Ec2

Discussão em 'Angular' iniciado por Dnyaneshwar Jadhav, Outubro 1, 2024.

  1. I am using below code in my controller

    @GetMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Resource> export(HttpServletResponse httpResponse
    ) {
    try {

    List<MyOutPut> data = mydata();
    List<String> fieldsToInclude = List.of("id", "Name");
    ExportRequest<MyOutPut, ExcelDesignOption> request = new ExportRequest<>(
    ExportRequest.ExportFormat.EXCELSX, data, fieldsToInclude, MyOutPut.class, "export_data", new ExcelDesignOption()
    );

    System.out.println("Data received now");
    ExportUseCase<MyOutPut, ExcelDesignOption> exportUseCase = new ExportUseCase<>();
    ExportResponse response = exportUseCase.execute(request);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentDispositionFormData("attachment", response.getFileName());
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    // httpResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    System.out.println("Data received now about to render from vendor");
    // Return the file as a byte array response entity

    // org.apache.commons.io.IOUtils.copy(response.getInputStream(), httpResponse.getOutputStream());
    // httpResponse.flushBuffer();
    InputStreamResource file = new InputStreamResource(response.getInputStream());

    return ResponseEntity.ok()
    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "export_data.xlsx")
    .header(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    .contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(file);
    } catch (Exception e) {
    log.error("search operation failed ", e);
    throw new RuntimeException("Error exporting Excel file", e);
    }
    }


    This works locally on my windows machine, but when I deploy this on AWS Ec2, and integrate the API from Angular UI, file gets downloaded but not opening.

    this is how in angular integrated exporting file from server

    .downloadExcel(this.id).subscribe({
    next: ({ blob, filename }) =>
    this.myService.handleFileDownload(blob, filename),
    error: (err) => console.error('File download failed', err)
    });

    return this.http.get(url, {
    observe: 'response',
    responseType: 'blob'
    }).pipe(
    map(response => {
    const contentDisposition = response.headers.get('Content-Disposition');
    const filename = contentDisposition
    ? contentDisposition.split('filename=')[1].replace(/"/g, '')
    : 'Registered Vendor List.xlsx';

    return { blob: response.body as Blob, filename };
    })
    );



    File structure [​IMG]

    Error [​IMG]

    Continue reading...

Compartilhe esta Página