DeepWorks Studios Blazor Component Library

Demo user Administrator ยท Blazor Server Library 20261206222504-6e8ff32.dirty

DwsFileDownload

A download button for a URL, several URLs, bytes or a stream. While a stream is sent, a DwsProgressBar shows how far it is (indeterminate while the length is unknown) next to a cancel button.

API Reference

Single file

Downloading one file from a URL. Cross-origin URLs must allow CORS, since the file bytes are fetched by the browser to force a real save instead of navigating away.

Show Code
FileDownload.razor
<DwsFileDownload Name="FileDownloadDemo_Single" SingleFileUrl="images/blackhole_black.png" ButtonText="Download Single File"
                 OnDownloadStarted="@(() => OnDownloadStarted("Single file"))"
                 OnDownloadCompleted="@(() => OnDownloadCompleted("Single file"))"
                 OnDownloadFailed="OnDownloadFailed" />
FileDownload.razor.cs

@inject IDwsSnackbarNotificationService SnackbarNotification

private void OnDownloadStarted(string label) => SnackbarNotification.ShowSnackbar($"{label} download started", DwsSnackbarLevel.Info);

private void OnDownloadCompleted(string label) => SnackbarNotification.ShowSnackbar($"{label} download finished", DwsSnackbarLevel.Success);

private void OnDownloadFailed(Exception ex) => SnackbarNotification.ShowSnackbar($"Download failed: {ex.Message}", DwsSnackbarLevel.Error);

Multiple files

MultipleFileUrls downloads several files in sequence, one browser download per URL. The browser may ask once whether the site may download multiple files.

Show Code
FileDownload.razor
<DwsFileDownload Name="FileDownloadDemo_MultipleButton" MultipleFileUrls="@_multipleUrls" ButtonText="Download Multiple Files"
                 OnDownloadStarted="@(() => OnDownloadStarted("Multiple files"))"
                 OnDownloadCompleted="@(() => OnDownloadCompleted("Multiple files"))"
                 OnDownloadFailed="OnDownloadFailed" />
FileDownload.razor.cs
private readonly List<string> _multipleUrls = ["images/blackhole_black.png", "icon-families.json", "icons.json"];

@inject IDwsSnackbarNotificationService SnackbarNotification

private void OnDownloadStarted(string label) => SnackbarNotification.ShowSnackbar($"{label} download started", DwsSnackbarLevel.Info);

private void OnDownloadCompleted(string label) => SnackbarNotification.ShowSnackbar($"{label} download finished", DwsSnackbarLevel.Success);

private void OnDownloadFailed(Exception ex) => SnackbarNotification.ShowSnackbar($"Download failed: {ex.Message}", DwsSnackbarLevel.Error);

Appearance & theming

Color, appearance, and size are driven by the same CSS-variable theming used across the library.

Show Code
FileDownload.razor
<DwsFileDownload Name="FileDownloadDemo_Outline" SingleFileUrl="images/blackhole_black.png" ButtonText="Outline"
                 Appearance="DwsAppearance.Outline" Color="DwsColor.Secondary" Class="me-2" />
<DwsFileDownload Name="FileDownloadDemo_Small" SingleFileUrl="images/blackhole_black.png" ButtonText="Small"
                 ButtonSize="DwsSize.Small" Color="DwsColor.Success" />

Interface-Description

Name Typ Default Description
IsDownloading bool - Whether a download is running.
Progress DwsFileDownloadProgress? - BytesTransferred, TotalBytes and Percent of the running or last stream download; null for URL and text downloads.
HasFailed bool - Whether the last download failed (cleared when the next one starts).
Name Typ Parameters Description
TriggerDownload Task Downloads whichever source is currently configured (SingleFileUrl, then MultipleFileUrls, then FileContent).
Download(DwsFileContent) Task Sets FileContent and immediately downloads it.
Download(DwsFileStreamContent) Task Downloads a file from a stream (file name, stream, content type, optional length, LeaveOpen). The stream is disposed afterwards unless LeaveOpen is set.
DownloadAsZip(IEnumerable<DwsFileStreamContent>, string zipFileName = "download.zip", CompressionLevel = Fastest) Task Packs the files into one zip archive (built in memory) and downloads it as a single file.
CreateZipAsync(IEnumerable<DwsFileStreamContent>, CompressionLevel, CancellationToken) static Task<MemoryStream> Builds the archive DownloadAsZip sends, positioned at its start. Entry names are made safe (no drive, no root, no '..') and unique.
CancelDownload Task Stops the running download: the stream is read no further, a URL fetch is aborted, nothing is saved. OnDownloadCancelled is raised.
Name Typ Default Description
SingleFileUrl string? null URL of a single file to download.
MultipleFileUrls List<string>? null List of file URLs to download in sequence.
FileContent DwsFileContent? null In-memory file content (name, content, content type) to download.
FileStreamProvider Func<CancellationToken, Task<DwsFileStreamContent?>>? null Produces the file as a stream when the download starts. Sent to the browser in chunks (DotNetStreamReference), with progress and cancel. The token is cancelled by CancelDownload; returning null fails the download. Used when no URL and no FileContent is set.
ShowProgress bool true Shows a DwsProgressBar (indeterminate while the length is unknown) with the transferred size under the button while a stream download runs longer than TransferUiDelay. URL downloads are fetched by the browser and report no progress.
ShowCancelButton bool true Shows a Cancel button while a download (stream or URL) runs longer than TransferUiDelay.
TransferUiDelay int 400 Milliseconds a download must run before the progress bar and the Cancel button appear, so a quick download does not flash them.
ButtonText string? null Text displayed on the download button. Falls back to a localized "Download" when not set.
ButtonIcon string Icons.BS.Download Icon displayed on the download button.
IsHeadless bool false When true, no button is rendered; the download is triggered programmatically via TriggerDownload/Download.
ShowErrorMessage bool true Shows a short localized message under the button when a download failed, until the next attempt. HasFailed tells the same in code.
Appearance DwsAppearance Filled Visual appearance of the download button.
Color DwsColor Primary Theme color of the download button.
ButtonSize DwsSize Medium Size of the download button.
Name Typ Description
OnDownloadProgress EventCallback<DwsFileDownloadProgress> Raised a few times per second while a stream download is sent, and once at its end.
OnDownloadCancelled EventCallback Raised when a download was stopped by CancelDownload or the Cancel button. Neither OnDownloadCompleted nor OnDownloadFailed follows.
OnDownloadStarted EventCallback Triggered when a download begins.
OnDownloadCompleted EventCallback Triggered when a download finishes successfully.
OnDownloadFailed EventCallback<Exception> Triggered when a download fails.
Name Typ Default Description
Element ElementReference The associated web component. May be null if accessed before the component is rendered.
ComponentGuid Guid Unique identifier for the component.
TypeString string The name of the component type, obtained from reflection.
Authorized_RegisterEnabled bool false Indicates whether this component is enabled through a permission registry.
Authorized_RegisterSelected bool false Indicates whether this component is selected in permission selection mode.
GetInitializedState bool false Returns whether the component is initialized.
GetRenderModeState DwsRenderMode Returns the current render mode.
GetComponentState DwsComponentStateType Returns the current component state.
GetIsInteractiveState bool false Returns whether the component is interactive.
GetIsPrerenderingState bool false Returns whether the component is prerendering.
GetComponentLoadingState bool false Returns whether the component is loading.
IsFormBusy bool false Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState).
Name Typ Parameters Description
DebugInfo void Logs debug information about the component.
UpdateState void Updates the component's state and triggers StateHasChanged.
UpdateStateAsync Task Asynchronously updates the component's state and triggers StateHasChanged.
Dispose void Disposes the component and releases resources.
FocusAsync ValueTask bool containedInScrollableList Sets focus to the component's element.
Name Typ Default Description
Name string Required unique identifier for the component within its container.
Visible bool true Determines if the component is visible in the UI.
Disabled bool false Disables the form control, ensuring it doesn't participate in form submission.
ComponentNameSpace string The namespace for permission-based access control.
PermissionKey string? null A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins.
Class string Optional CSS class names.
Style string Optional in-line styles.
Tag object An optional user-defined object to store any additional data with the component.
BackReference DwsReference A reference to the enclosing component.
ID string The HTML ID attribute for the component.
AdditionalAttributes Dictionary<string, object> Additional HTML attributes for the component's root element.
IgnorePermissionChecks bool false Bypasses all permission checks for this component.
IgnorePermissionSelection bool false Ignore permission selection mode for this component.
IgnoreFormBusy bool false Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it.
Name Typ Description
OnInitializedCallbackStarted EventCallback Callback when OnInitialized starts.
OnInitializedCallback EventCallback Callback during OnInitialized.
OnInitializedCallbackCompleted EventCallback Callback when OnInitialized completes.
OnInitializedAsyncCallbackStarted EventCallback Callback when OnInitializedAsync starts.
OnInitializedAsyncCallback EventCallback Callback during OnInitializedAsync.
OnInitializedAsyncCallbackCompleted EventCallback Callback when OnInitializedAsync completes.
OnIsComponentLoadingChanged EventCallback<bool> Callback when IsComponentLoading changes.
OnCurrentRenderModeChanged EventCallback<DwsRenderMode> Callback when CurrentRenderMode changes.
OnComponentStateChanged EventCallback<DwsComponentStateType> Callback when ComponentState changes.
An error has occurred. This app may no longer respond until reloaded.