Skip to content

DNF-SaS/BlazorFileReader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Build status

BlazorFileReader

Blazor component and Demo of file streams in Blazor.

This demo exposes read-only streams using a Blazor component that wraps <input type="file" /> and FileReader.

Here is a Live demo that contains the output of this project (master branch compiled in Release configuration).

Usage

@page "/MyPage"
@using System.IO;
@using BlazorFileReader.FileReaderComponent;

<FileReader ReaderRef="@MyFileReaderReference" /><button onclick="@ReadFile">Read file</button>

@functions {
    IFileReaderRef MyFileReaderReference;
    protected override void OnInit()
    {
        MyFileReaderReference = FileReaderReference.Create();
        base.OnInit();
    }
    public async Task ReadFile()
    {
        foreach (var file in await MyFileReaderReference.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, ...)
            }
        }
    }
}

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

About

File streams in Blazor - Component and Demo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 74.8%
  • HTML 12.8%
  • TypeScript 8.6%
  • CSS 3.8%