Skip to content

Read-only File streams in Blazor - Library and Demo

License

Notifications You must be signed in to change notification settings

cocis48/BlazorFileReader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Build status NuGet NuGet

BlazorFileReader

Blazor library and Demo of read-only file streams in Blazor. Originally built for Wasm ("Client-side" Blazor), Server-side Blazor (previously aka RazorComponents) is also supported as of version 0.7.1.

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

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.10.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.

Client-side / Wasm Project type

Use Nuget: Install-Package Tewr.Blazor.FileReader.Wasm

Setup IoC for IFileReaderServiceas in (Startup.cs):

services.AddFileReaderService();

You must manually include the javascript required due to a missing feature in Server components Download FileReaderComponent.js (or extract it from the nuget package content folder) and reference it in wwwroot/index.html after the line

<script src="_framework/components.server.js"></script>

Server-side / asp.net core Project type

Use Nuget: Install-Package Tewr.Blazor.FileReader

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

services.AddScoped<IFileReaderService, FileReaderService>();

You must manually include the javascript required due to a missing feature in Server components Download FileReaderComponent.js (or extract it from the nuget package content folder) and reference it in Pages/_Host.cshtml after the line

<script src="_framework/components.server.js"></script>

Usage in a Blazor View

The code for views looks the same for both client- and server-side projects, but take a look at known issues for 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, ...)
            }
        }
    }
}

Known issues

As of dotnet sdk 3.0.100-preview4, Server-side blazor has a problem with "big" messages. For now, buffersize must be set, and must be set to something quite low for this to work without crashes. 2k bytes seems to work alright in chrome, YMMV.

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-preview-4-19216-03 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.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.

About

Read-only File streams in Blazor - Library and Demo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 38.0%
  • C# 32.0%
  • TypeScript 24.4%
  • CSS 5.6%