Skip to content
Read-only File streams in Blazor - Library and Demo
Branch: master
Clone or download
KNUTSSON Tor
KNUTSSON Tor Readme update
Latest commit e8f3024 Jun 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Issue 42 (#43) Jun 16, 2019
.gitignore Initial commit Jun 6, 2018
LICENSE Initial commit Jun 6, 2018
README.md Readme update Jun 18, 2019
icon.png Upgrade to preview 6 (#41) Jun 16, 2019
icon.svg Upgrade to preview 6 (#41) Jun 16, 2019

README.md

Build status NuGet Donate

BlazorFileReader

Blazor library exposing read-only file streams in Blazor.

This library exposes read-only streams using <input type="file" /> and FileReader.

Originally built for Wasm ("Client-side" Blazor), Server-side Blazor (previously aka RazorComponents) is also supported as of version 0.7.1.

Here is a Live demo that contains the output of the wasm demo project. Currently, its a build based on v0.5.1.

Installation

0.12.0 is a pre-release version. First of all, make sure your environment is up to date with the appropriate SDK and VS2019 preview 4. See this article for more details. Depending on your project type, use one of the two examples below. For a complete use-case, see the client or server-side demo projects.

Client-side / Wasm Project type

Use Nuget: Install-Package Tewr.Blazor.FileReader

Setup IoC for IFileReaderServiceas in (Startup.cs):

services.AddFileReaderService();

Server-side / asp.net core Project type

Use Nuget: Install-Package Tewr.Blazor.FileReader

Setup IoC for IFileReaderService as in the example (Startup.cs):

services.AddFileReaderService(options => options.InitializeOnFirstCall = true);

Usage in a Blazor View

The code for views looks the same for both client- and server-side projects.

@page "/MyPage"
@using System.IO;
@inject IFileReaderService fileReaderService;

<input type="file" @ref="@inputTypeFileElement" /><button @onclick="@ReadFile">Read file</button>

@functions {
    ElementRef inputTypeFileElement;

    public async Task ReadFile()
    {
        foreach (var file in await fileReaderService.CreateReference(inputTypeFileElement).EnumerateFilesAsync())
        {
            // Read into buffer and act (uses less memory)
            using(Stream stream = await file.OpenReadAsync()) {
                // Do (async) stuff with stream...
                await stream.ReadAsync(buffer, ...);
                // The following will fail. Only async read is allowed.
                stream.Read(buffer, ...)
            }

            // Read into memory and act
            using(MemoryStream memoryStream = await file.CreateMemoryStreamAsync(4096)) {
                // Sync calls are ok once file is in memory
                memoryStream.Read(buffer, ...)
            }
        }
    }
}

Notes

To use the code in this demo in your own project you need to use at least version 0.4.0 of blazor (see branch 0.4.0).

The master branch uses v3.0.0-preview6.19307.2 of Blazor.

Blazor is an experimental preview project, not ready for production use. Just as Blazor API frequently has breaking changes, so does the API of this library.

Version notes

Version 0.12.0 adds support for sdk 3.0.0-preview6.19307.2, and several issues are resolved with this release, notably meticulous setup and issues with buffer size for server-side projects. Also, the Wasm helper package has been deprecated.

Version 0.11.0 adds support for sdk 3.0.0-preview5-19227-01. It also introduces a tiny feature: The IFileReaderRef.ClearValue() method, used to clear the value of a referenced file input. Also, fixes a bug in Edge and a package issue.

Version 0.10.0 adds support for sdk v3.0.0-preview-4-19216-03

Versions 0.9.0 introduces a small helper-package for the IoC setup of Wasm, injecting an implementation of IInvokeUnmarshalled.

Versions 0.8.0 requires copy-paste implementation of IInvokeUnmarshalled.

Versions previous to 0.7.1 did not support server-side Blazor and would throw [System.PlatformNotSupportedException] Requires MonoWebAssemblyJSRuntime as the JSRuntime.

Versions previous to 0.5.1 wrapped the input element in a Blazor Component, this has been removed for better configurability and general lack of value.

You can’t perform that action at this time.