From d86cdc773caf092d8297110a85ea3a5d3fd68592 Mon Sep 17 00:00:00 2001 From: "Jade.VAN_BRABANDT" Date: Mon, 8 Jan 2024 18:49:05 +0100 Subject: [PATCH] fix : export --- Blazor/Blazor/Pages/Questions/Questions.razor | 5 ++++- Blazor/Blazor/Pages/Questions/Questions.razor.cs | 3 +-- Blazor/Blazor/Pages/Questions/Questions.razor.js | 11 +++++++++++ Blazor/Blazor/Pages/_Host.cshtml | 2 +- Blazor/Blazor/Pages/_Layout.cshtml | 2 +- 5 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Blazor/Blazor/Pages/Questions/Questions.razor.js diff --git a/Blazor/Blazor/Pages/Questions/Questions.razor b/Blazor/Blazor/Pages/Questions/Questions.razor index 3d04eb3..b5fc630 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor +++ b/Blazor/Blazor/Pages/Questions/Questions.razor @@ -30,4 +30,7 @@ - \ No newline at end of file + + + + \ No newline at end of file diff --git a/Blazor/Blazor/Pages/Questions/Questions.razor.cs b/Blazor/Blazor/Pages/Questions/Questions.razor.cs index 37c6f5a..9922241 100644 --- a/Blazor/Blazor/Pages/Questions/Questions.razor.cs +++ b/Blazor/Blazor/Pages/Questions/Questions.razor.cs @@ -110,8 +110,7 @@ public partial class Questions } } - var sentFile = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())); - + var sentFile = new MemoryStream(Encoding.Unicode.GetBytes(sb.ToString())); using (var streamRef = new DotNetStreamReference(stream: sentFile)) { await IJSRuntime.InvokeVoidAsync("downloadFileFromStream", "data.csv", streamRef); diff --git a/Blazor/Blazor/Pages/Questions/Questions.razor.js b/Blazor/Blazor/Pages/Questions/Questions.razor.js new file mode 100644 index 0000000..37f904d --- /dev/null +++ b/Blazor/Blazor/Pages/Questions/Questions.razor.js @@ -0,0 +1,11 @@ +window.downloadFileFromStream = async (fileName, contentStreamReference) => { + const arrayBuffer = await contentStreamReference.arrayBuffer(); + const blob = new Blob([arrayBuffer]); + const url = URL.createObjectURL(blob); + const anchorElement = document.createElement('a'); + anchorElement.href = url; + anchorElement.download = fileName ?? ''; + anchorElement.click(); + anchorElement.remove(); + URL.revokeObjectURL(url); +} \ No newline at end of file diff --git a/Blazor/Blazor/Pages/_Host.cshtml b/Blazor/Blazor/Pages/_Host.cshtml index f89e313..fbf07cd 100644 --- a/Blazor/Blazor/Pages/_Host.cshtml +++ b/Blazor/Blazor/Pages/_Host.cshtml @@ -29,7 +29,7 @@ Reload 🗙 - + diff --git a/Blazor/Blazor/Pages/_Layout.cshtml b/Blazor/Blazor/Pages/_Layout.cshtml index c8c28a0..7d6cf4d 100644 --- a/Blazor/Blazor/Pages/_Layout.cshtml +++ b/Blazor/Blazor/Pages/_Layout.cshtml @@ -10,7 +10,7 @@ - +