nukedbit / BlazorFileReader

Read-only File streams in Blazor - Library and Demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build status NuGet NuGet

BlazorFileReader

Blazor library and Demo of read-only file streams in Blazor.

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

Use Nuget: Install-Package Tewr.Blazor.FileReader -Version 0.7.1

The installation guide below concerns v0.7.1. For use with Blazor 0.8.0, see branch release/0.8.0-preview

Usage

Depending on your project type, use one of the two examples below.

Client-side / Wasm Project type

Setup IoC for IFileReaderService in (Program.cs):

   using Blazor.FileReader;
   static void Main(string[] args)
   {
       var serviceProvider = new BrowserServiceProvider(services =>
       {
            services.AddSingleton<IFileReaderService, FileReaderService>();
       });
       new BrowserRenderer(serviceProvider).AddComponent<App>("app");
       (...)

Server-side / asp.net core Project type

Functionality only available in v0.7.1

Setup IoC for IFileReaderService in (Startup.cs):

using Blazor.FileReader;

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IFileReaderService, FileReaderService>();
        }
	(...)

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 stuff with stream...
                await stream.ReadAsync(buffer, ...);
		// This 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 (branch 0.4.0).

The master branch uses 0.7.1 of Blazor.

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

Version notes

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

License:MIT License


Languages

Language:HTML 49.0%Language:C# 35.9%Language:TypeScript 8.3%Language:CSS 6.7%