diff --git a/src/main/java/com/primefactorsolutions/views/DocumentsListView.java b/src/main/java/com/primefactorsolutions/views/DocumentsListView.java index 101197d..229d333 100644 --- a/src/main/java/com/primefactorsolutions/views/DocumentsListView.java +++ b/src/main/java/com/primefactorsolutions/views/DocumentsListView.java @@ -18,6 +18,7 @@ import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; import org.vaadin.firitin.components.grid.PagingGrid; +import java.util.Base64; import java.util.List; @@ -52,7 +53,7 @@ public class DocumentsListView extends Main { documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee"); addDocumentActionColumn("View", this::navigateToDocumentView); addDocumentActionColumn("Edit", this::navigateToEditDocumentView); - addDocumentActionColumn("Download", this::navigateToDownloadDocumentView); + addDocumentActionColumn("Download", this::downloadDocument); configurePagination(); } @@ -136,6 +137,26 @@ public class DocumentsListView extends Main { sortOrder.getDirection() == SortDirection.ASCENDING); } + private void downloadDocument(final Document document) { + String base64Data = Base64.getEncoder().encodeToString(document.getFileData()); + String jsCode = "var byteCharacters = atob('" + base64Data + "');" + + "var byteNumbers = new Array(byteCharacters.length);" + + "for (var i = 0; i < byteCharacters.length; i++) {" + + " byteNumbers[i] = byteCharacters.charCodeAt(i);" + + "}" + + "var byteArray = new Uint8Array(byteNumbers);" + + "var blob = new Blob([byteArray], { type: 'application/pdf' });" + + "var url = URL.createObjectURL(blob);" + + "var a = document.createElement('a');" + + "a.href = url;" + + "a.download = '" + document.getFileName() + "';" + + "document.body.appendChild(a);" + + "a.click();" + + "document.body.removeChild(a);" + + "URL.revokeObjectURL(url);"; + getElement().executeJs(jsCode); + } + @FunctionalInterface private interface DocumentActionHandler { void handle(Document document);