using System.Text;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace SharepointToolbox.Web.Services.Export;
///
/// Triggers browser file downloads from Blazor Server components.
/// Converts string export outputs to bytes and invokes JS download.
///
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));
}
///
/// Downloads pre-encoded bytes (e.g. a ZIP or a merged report produced by
/// ) with an explicit MIME type.
///
public async Task DownloadBytesAsync(byte[] content, string fileName, string mime)
{
await _js.InvokeVoidAsync("sptb.downloadFile", fileName, mime, Convert.ToBase64String(content));
}
}