d69c3290d8
Reviewed-on: #2
38 lines
1.4 KiB
C#
38 lines
1.4 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));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Downloads pre-encoded bytes (e.g. a ZIP or a merged report produced by
|
|
/// <see cref="ReportMergeHelper"/>) with an explicit MIME type.
|
|
/// </summary>
|
|
public async Task DownloadBytesAsync(byte[] content, string fileName, string mime)
|
|
{
|
|
await _js.InvokeVoidAsync("sptb.downloadFile", fileName, mime, Convert.ToBase64String(content));
|
|
}
|
|
}
|