How to render multiple documents in single page in browser using spring boot

Viewed 105

In my application Employee(Entity) can have many Document(Entity). An employee can upload his documents on my website. I want to give a view option to the employees to see all his documents. I can show only one document in the browser whenever the user clicks on the view button. But I don't know how to show all documents all together in the browser whenever a user clicks on the view button.

Here is my code for showing only one document.

@GetMapping("/showDocument")
    public ResponseEntity<byte[]> showDocument(
            @RequestParam("employeeId") int employeeId
    ) throws IOException, SQLException {

        Employee employee = employeeService.findById(employeeId);
        HttpHeaders httpHeaders = new HttpHeaders();
        ResponseEntity<byte[]> responseEntity = null;
        if (employee != null && employee.getDocuments() != null) {
            Document document = employee.getDocuments().get(0);
            InputStream inputStream = document.getContent().getBinaryStream();
            httpHeaders.setCacheControl(CacheControl.noCache().getHeaderValue());
            httpHeaders.set(CONTENT_TYPE, document.getContentType());
            byte[] media = IOUtils.toByteArray(inputStream);
            responseEntity = new ResponseEntity<>(media, httpHeaders, HttpStatus.OK);
        }
        return responseEntity;
    }
1 Answers

if I had to design this solution, I would not fetch all the documents in one call as this would be heavy on network and would be slower as well. Infact I would just fetch the metadata to show documents in form of thumbnails and on demand fetch the specific document when a user click on specific thumbnail.

Related