BlazorDownloadFile

repository·master·Indexed 41 days ago

https://github.com/arivera12/blazordownloadfile

A library that allows Blazor developers to trigger file downloads to the browser directly from C# code without requiring external JavaScript libraries or manual JS interop management. Supports downloading via Base64 strings, byte arrays, streams, and plain text, and includes functionality for incremental data buffering via AddBuffer for handling large files.

Tokens
2.4K
Snippets
14
Records
16
Agent score
50%

What's inside BlazorDownloadFile

  1. Performance and limitations of BlazorDownloadFile

    master

    Performance

    • Base64 vs Stream: base64 strings and byte[] generally perform faster than Stream because there is no direct conversion between a C# Stream and a JavaScript object.
    • Binary Representation: The library uses Uint8Array to push binary data into a JavaScript Blob, which performs well.

    Limitations

  2. Configure Content-Security-Policy (CSP) for BlazorDownloadFile

    master

    If your application uses a Content-Security-Policy with script-src 'self', you must manually include the library's JavaScript file in your HTML to ensure the download functionality works. Add the following script tag to your index/root HTML file:

    <script src="_content/BlazorDownloadFile/BlazorDownloadFileScript.js"></script>
  3. Use Open Iconic SVG Sprites

    master

    Open Iconic provides an SVG sprite that allows you to load all icons in a single HTTP request. To use an icon, embed a <use> element inside an <svg> tag, referencing the specific icon ID from the sprite file.

    Implementation

    To ensure icons are easily stylable, it is recommended to add a general class to the <svg> tag and a unique class name to the <use> tag.

    <svg class="icon">
      <use xlink:href="open-iconic.svg#account-login" class="icon-account-login"></use>
    </svg>

    Styling

    Sizing

    Since all icons are square, set equal width and height dimensions on the <svg> tag:

    .icon {
      width: 16px;
      height: 16px;
    }

    Coloring

    Use the CSS fill property on the <use> tag to change the icon color:

    .icon-account-login {
      fill: #f00;
    }
  4. Use Open Iconic Icon Fonts

    master

    Open Iconic provides webfont stylesheets for standalone use or integration with Bootstrap and Foundation frameworks.

    Standalone Usage

    Include the default stylesheet and use the oi class with the data-glyph attribute.

    1. Link the CSS:
    <link href="/open-iconic/font/css/open-iconic.css" rel="stylesheet">
    1. Use the icon:
    <span class="oi" data-glyph="icon-name" title="icon name" aria-hidden="true"></span>

    Bootstrap Integration

    Use the Bootstrap-specific stylesheet and the oi oi-icon-name class pattern.

    1. Link the CSS:
    <link href="/open-iconic/font/css/open-iconic-bootstrap.css" rel="stylesheet">
    1. Use the icon:
    <span class="oi oi-icon-name" title="icon name" aria-hidden="true"></span>

    Foundation Integration

    Use the Foundation-specific stylesheet and the fi-icon-name class pattern.

    1. Link the CSS:
    <link href="/open-iconic/font/css/open-iconic-foundation.css" rel="stylesheet">
    1. Use the icon:
    <span class="fi-icon-name" title="icon name" aria-hidden="true"></span>
  5. Register the BlazorDownloadFile service

    master

    Register the BlazorDownloadFileService in your application's dependency injection container within the services method.

    Important: ServiceLifetime.Singleton is not supported. Use ServiceLifetime.Scoped (the default) or ServiceLifetime.Transient.

    services.AddBlazorDownloadFile(ServiceLifetime.Scoped);
  6. Merge and download pending buffers

    master

    If you have used AddBuffer to incrementally load data, you must call one of the following methods to merge those buffers and trigger the browser download:

    • DownloadBase64Buffers: Use this if the buffers added via AddBuffer were Base64 strings.
    • DownloadBinaryBuffers: Use this if the buffers added via AddBuffer were byte[], IEnumerable<byte>, or Stream types.

    Both methods require a fileName and an optional contentType (defaults to application/octet-stream).

    // Example: Merging binary buffers and downloading
    await BlazorDownloadFileService.DownloadBinaryBuffers("largefile.zip");