Feat : Finished CSV export, code might not be clean
continuous-integration/drone/push Build is passing Details

pull/37/head
Jade VAN BRABANDT 1 year ago
parent 77f5a566e9
commit 00cb290649

@ -12,3 +12,5 @@
<DataGridColumn TItem="ChaptersModel" Field="@nameof(ChaptersModel.Id)" Caption="#" />
<DataGridColumn TItem="ChaptersModel" Field="@nameof(ChaptersModel.Name)" Caption="Name" />
</DataGrid>
<script src="Pages/Chapters.razor.js"></script>

@ -3,7 +3,7 @@ using Blazored.LocalStorage;
using Blazorise.DataGrid;
using ChoETL;
using Microsoft.AspNetCore.Components;
using System.Reflection.Metadata;
using Microsoft.JSInterop;
using System.Text;
namespace Blazor.Pages;
@ -25,6 +25,9 @@ public partial class Chapters
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public IJSRuntime IJSRuntime { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Do not treat this action if is not the first render
@ -75,19 +78,11 @@ public partial class Chapters
}
}
var sentFile = new MemoryStream(Encoding.ASCII.GetBytes(sb.ToString()));
string path = @"C:\temp\file"; // path to file
using (FileStream fs = File.Create(path))
using (var streamRef = new DotNetStreamReference(stream: sentFile))
{
// writing data in string
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
fs.Write(info, 0, info.Length);
// writing data in bytes already
byte[] data = new byte[] { 0x0 };
fs.Write(data, 0, data.Length);
await IJSRuntime.InvokeVoidAsync("downloadFileFromStream", "data.csv", streamRef);
}
}
}

@ -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);
}
Loading…
Cancel
Save