29 lines
1.0 KiB
C#
29 lines
1.0 KiB
C#
using System.Text;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.JSInterop;
|
|
|
|
namespace SharepointToolbox.Web.Services.Export;
|
|
|
|
/// <summary>
|
|
/// Triggers browser file downloads from Blazor Server components.
|
|
/// Converts string export outputs to bytes and invokes JS download.
|
|
/// </summary>
|
|
public class WebExportService
|
|
{
|
|
private readonly IJSRuntime _js;
|
|
|
|
public WebExportService(IJSRuntime js) { _js = js; }
|
|
|
|
public async Task DownloadCsvAsync(string content, string fileName)
|
|
{
|
|
var bytes = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true).GetBytes(content);
|
|
await _js.InvokeVoidAsync("sptb.downloadFile", fileName, "text/csv;charset=utf-8", Convert.ToBase64String(bytes));
|
|
}
|
|
|
|
public async Task DownloadHtmlAsync(string content, string fileName)
|
|
{
|
|
var bytes = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes(content);
|
|
await _js.InvokeVoidAsync("sptb.downloadFile", fileName, "text/html;charset=utf-8", Convert.ToBase64String(bytes));
|
|
}
|
|
}
|