TusBlazorClient 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TusBlazorClient --version 1.0.0                
NuGet\Install-Package TusBlazorClient -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TusBlazorClient" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TusBlazorClient --version 1.0.0                
#r "nuget: TusBlazorClient, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TusBlazorClient as a Cake Addin
#addin nuget:?package=TusBlazorClient&version=1.0.0

// Install TusBlazorClient as a Cake Tool
#tool nuget:?package=TusBlazorClient&version=1.0.0                

tus-blazor-client

<img alt="Tus logo" src="https://github.com/tus/tus.io/blob/main/public/images/tus1.png?raw=true" width="30%" align="right" />

tus is a protocol based on HTTP for resumable file uploads. Resumable means that an upload can be interrupted at any moment and can be resumed without re-uploading the previous data again. An interruption may happen willingly, if the user wants to pause, or by accident in case of an network issue or server outage.

tus-blazor-client is a wrapper library project for tus-js-client that can be used in .NET Blazor.

Why do I use this?

The file I/O speed in .NET Blazor is not suitable for sending large files, and there are also limitations on the size of files that can be transferred. Therefore, sending large files with pure C# code in Blazor can be a challenging task.

Installation

Install tus-js-client


...
    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>
    <script src="_framework/blazor.webassembly.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/tus-js-client@latest/dist/tus.min.js"></script>

Install tus-blazor-client

use Nuget dotnet add package TusBlazorClient --version 1.0.0

Add TusBlazorClient in ServiceProvider

// project/Program.cs
...
builder.Services.AddTusBlazorClient();
await builder.Build().RunAsync();

Example

@inject TusClient TusClient

<input type="file" @ref="_fileElement"/>
<button onclick="@Upload">upload</button>

@code {
    private ElementReference _fileElement;
    private TusUpload? _tusUpload;
    
    private async Task Upload()
    {
        // Get the selected file from the input element
        var file =  (await TusClient.GetFileInputElement(_fileElement).GetFiles()).First();
        // Get the selected file's info
        var fileInfo = await file.GetFileInfo();
        // Create a new tus upload
        var opt = new TusOptions
        {
            Endpoint = new Uri("http://localhost:1080/files"),
            Metadata = new Dictionary<string, string>()
            {
                {"filename", fileInfo.Name},
            },
            OnError = (err) =>
            {
                Console.WriteLine($"Failed because: {err.ErrorMessage}");
            },
            OnProgress = (bytesUploaded, bytesTotal) =>
            {
                var percentage = (double)bytesUploaded / bytesTotal;
                Console.WriteLine($"{bytesUploaded} {bytesTotal} {percentage:F}%");
            },
            OnSuccess = async () =>
            {
                var url = await _tusUpload.GetUrl();
                Console.WriteLine($"Download {fileInfo.Name} from {url}");
            },
        };
        _tusUpload = await TusClient.Upload(file, opt);
        
        // Check if there are any previous uploads to continue.
        var previousUploads = await _tusUpload.FindPreviousUpload();
        if (previousUploads.Count > 0)
        {
            // Found previous uploads so we select the first one.
            await _tusUpload.ResumeFromPreviousUpload(previousUploads.First());
        }
        
        // Start the upload
        await _tusUpload.Start();
    }
}

API

Check Wiki

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 1,165 10/24/2023
1.0.0 447 10/23/2023

1.0.0