diff --git a/Client/Pages/Index.razor.cs b/Client/Pages/Index.razor.cs index 8b95a79..b7ee35b 100644 --- a/Client/Pages/Index.razor.cs +++ b/Client/Pages/Index.razor.cs @@ -104,8 +104,7 @@ namespace ShiftScheduler.Client.Pages var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", shiftsWithTransportList); var bytes = await response.Content.ReadAsByteArrayAsync(); var base64 = Convert.ToBase64String(bytes); - var fileUrl = $"data:application/octet-stream;base64,{base64}"; - NavigationManager.NavigateTo(fileUrl, true); + await JSRuntime.InvokeVoidAsync("downloadFile", "schedule.pdf", "application/pdf", base64); } private async Task SaveScheduleToStorage() diff --git a/Client/wwwroot/index.html b/Client/wwwroot/index.html index 9dead99..cfa7310 100644 --- a/Client/wwwroot/index.html +++ b/Client/wwwroot/index.html @@ -26,6 +26,7 @@ Reload 🗙 + diff --git a/Client/wwwroot/js/fileDownload.js b/Client/wwwroot/js/fileDownload.js new file mode 100644 index 0000000..a0c444b --- /dev/null +++ b/Client/wwwroot/js/fileDownload.js @@ -0,0 +1,18 @@ +window.downloadFile = (filename, contentType, base64Data) => { + const byteCharacters = atob(base64Data); + const byteNumbers = new Array(byteCharacters.length); + for (let i = 0; i < byteCharacters.length; i++) { + byteNumbers[i] = byteCharacters.charCodeAt(i); + } + const byteArray = new Uint8Array(byteNumbers); + const blob = new Blob([byteArray], { type: contentType }); + + const url = window.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = filename; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + window.URL.revokeObjectURL(url); +}; \ No newline at end of file diff --git a/test.pdf b/test.pdf new file mode 100644 index 0000000..c6fb7eb Binary files /dev/null and b/test.pdf differ