Fix PDF export filename: Add .pdf extension to downloaded files (#13)
* Initial plan * Fix PDF export filename: use JavaScript download with proper .pdf extension Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com> * Remove test PDF file from repository Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com> * Add year and month --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com> Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
This commit is contained in:
parent
2dc437ac9c
commit
b41d8d6b36
3 changed files with 20 additions and 2 deletions
|
|
@ -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 {EditYear}-{EditMonth:00}.pdf", "application/pdf", base64);
|
||||
}
|
||||
|
||||
private async Task SaveScheduleToStorage()
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
<a href="" class="reload">Reload</a>
|
||||
<a class="dismiss">🗙</a>
|
||||
</div>
|
||||
<script src="js/fileDownload.js"></script>
|
||||
<script src="_framework/blazor.webassembly.js"></script>
|
||||
</body>
|
||||
|
||||
|
|
|
|||
18
Client/wwwroot/js/fileDownload.js
Normal file
18
Client/wwwroot/js/fileDownload.js
Normal file
|
|
@ -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);
|
||||
};
|
||||
Loading…
Reference in a new issue