|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +using Microsoft.JSInterop; |
| 5 | + |
| 6 | +namespace LoreSoft.Blazor.Controls; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Provides methods for downloading files in Blazor applications using JavaScript interop. |
| 10 | +/// </summary> |
| 11 | +public class DownloadService(IJSRuntime javaScript) |
| 12 | +{ |
| 13 | + private readonly IJSRuntime _javaScript = javaScript ?? throw new ArgumentNullException(nameof(javaScript)); |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Downloads a file from a provided <see cref="Stream"/> to the user's device. |
| 17 | + /// </summary> |
| 18 | + /// <param name="stream">The stream containing the file data.</param> |
| 19 | + /// <param name="fileName">The name of the file to be downloaded. Optional.</param> |
| 20 | + /// <param name="mimeType">The MIME type of the file. Optional.</param> |
| 21 | + /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> |
| 22 | + /// <exception cref="ArgumentNullException">Thrown if <paramref name="stream"/> is null.</exception> |
| 23 | + public async Task DownloadFileStream(Stream stream, string? fileName = null, string? mimeType = null) |
| 24 | + { |
| 25 | + ArgumentNullException.ThrowIfNull(stream); |
| 26 | + |
| 27 | + using var streamReference = new DotNetStreamReference(stream, true); |
| 28 | + |
| 29 | + await _javaScript.InvokeVoidAsync("BlazorControls.downloadFileStream", streamReference, fileName, mimeType); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Downloads a text file to the user's device. |
| 34 | + /// </summary> |
| 35 | + /// <param name="text">The text content to be downloaded.</param> |
| 36 | + /// <param name="fileName">The name of the file to be downloaded. Optional.</param> |
| 37 | + /// <param name="mimeType">The MIME type of the file. Optional.</param> |
| 38 | + /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> |
| 39 | + /// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is null.</exception> |
| 40 | + public async Task DownloadFileText(string text, string? fileName = null, string? mimeType = null) |
| 41 | + { |
| 42 | + ArgumentNullException.ThrowIfNull(text); |
| 43 | + |
| 44 | + var bytes = Encoding.UTF8.GetBytes(text); |
| 45 | + await using var stream = new MemoryStream(bytes); |
| 46 | + |
| 47 | + // need to reset stream position |
| 48 | + stream.Seek(0, SeekOrigin.Begin); |
| 49 | + |
| 50 | + await DownloadFileStream(stream, fileName, mimeType); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Triggers a file download from a specified URL. |
| 55 | + /// </summary> |
| 56 | + /// <param name="url">The URL of the file to download.</param> |
| 57 | + /// <param name="fileName">The name of the file to be downloaded. Optional.</param> |
| 58 | + /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> |
| 59 | + /// <exception cref="ArgumentNullException">Thrown if <paramref name="url"/> is null.</exception> |
| 60 | + public async Task TriggerFileDownload(string url, string? fileName = null) |
| 61 | + { |
| 62 | + ArgumentNullException.ThrowIfNull(url); |
| 63 | + |
| 64 | + await _javaScript.InvokeVoidAsync("BlazorControls.triggerFileDownload", url, fileName); |
| 65 | + } |
| 66 | +} |
0 commit comments