• Stars
    star
    319
  • Rank 131,491 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 2 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Blazor wrapper for the File System Access browser API.

License: MIT GitHub issues GitHub forks GitHub stars NuGet Downloads (official NuGet)

Introduction

A Blazor wrapper for the browser API File System Access

The API makes it possible to read and write to your local file system from the browser both files and directories.

Disclaimer: The API is supported on a limited set of browsers. Most noticeable not supported on Firefox, Chrome for Android, and iOS mobile browsers.

Demo

The sample project can be demoed at https://kristofferstrube.github.io/Blazor.FileSystemAccess/

On each page you can find the corresponding code for the example in the top right corner.

On the main page you can see if the API has at least minimal support in the used browser.

On the Status page you can see how much of the WebIDL specs this wrapper has covered.

Getting Started

Prerequisites

You need to install .NET 7.0 or newer to use the library.

Download .NET 7

Installation

You can install the package via Nuget with the Package Manager in your IDE or alternatively using the command line:

dotnet add package KristofferStrube.Blazor.FileSystemAccess

Usage

The package can be used in Blazor WebAssembly and Blazor Server projects. (Note that streaming of big files is not supported in Blazor Server due to bandwidth problems.)

Import

You also need to reference the package in order to use it in your pages. This can be done in _Import.razor by adding the following.

@using KristofferStrube.Blazor.FileSystemAccess

Add to service collection

An easy way to make the service available in all your pages is by registering it in the IServiceCollection so that it can be dependency injected in the pages that need it. This is done in Program.cs by adding the following before you build the host and run it.

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

// Other services are added.

builder.Services.AddFileSystemAccessService();

await builder.Build().RunAsync();

Inject in page

Then the service can be injected in a page like so:

@inject IFileSystemAccessService FileSystemAccessService;

Then you can use IFileSystemAccessService to open one of the three dialogs available in the FileSystemAccess API like this:

<button @onclick="OpenAndReadFile">Open File Picker for Single File and Read</button>
<br />
@Text

@code {
    private string Text = "";

    private async Task OpenAndReadFile()
    {
        FileSystemFileHandle? fileHandle = null;
        try
        {
            OpenFilePickerOptionsStartInWellKnownDirectory options = new()
                {
                    Multiple = false,
                    StartIn = WellKnownDirectory.Downloads
                };
            var fileHandles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);
            fileHandle = fileHandles.Single();
        }
        catch (JSException ex)
        {
            // Handle Exception or cancelation of File Access prompt
            Console.WriteLine(ex);
        }
        finally
        {
            if (fileHandle is not null)
            {
                var file = await fileHandle.GetFileAsync();
                Text = await file.TextAsync();
                StateHasChanged();
            }
        }
    }
}

Issues

Feel free to open issues on the repository if you find any errors with the package or have wishes for features.

A known issue is that using Streams to stream large amount of data in Blazor Server is not supported.

Related repositories

This project uses the Blazor.FileSystem package to return rich FileSystemHandles both FileSystemFileHandes and FileSystemDirectoryHandles.

Related articles

This repository was build with inspiration and help from the following series of articles:

More Repositories

1

Blazor.SVGEditor

A basic SVG editor written in Blazor.
C#
297
star
2

Blazor.WebAudio

A Blazor wrapper for the Web Audio browser API.
C#
130
star
3

Blazor.WebAuthentication

A Blazor wrapper for the Web Authentication browser API.
C#
89
star
4

EditorConfigWizard

A Wizard for creating and editing EditorConfig files.
HTML
83
star
5

Blazor.MediaCaptureStreams

A Blazor wrapper for the Media Capture and Streams browser API.
C#
67
star
6

Blazor.GraphEditor

A simple graph editor for Blazor.
C#
67
star
7

Blazor.ServiceWorker

A Blazor wrapper for the Service Worker API.
C#
27
star
8

Blazor.FileAPI

A Blazor wrapper for the browser File API.
C#
27
star
9

Blazor.FileSystem

A Blazor wrapper for the File System browser API.
C#
26
star
10

Blazor.Popper

A Blazor wrapper for the Javascript library Popper.js
C#
25
star
11

DistributedDrawing

A distributed drawing tool written in Blazor Wasm that uses an external SignalR Hub to sync drawings between clients.
HTML
25
star
12

FlipGame

Flip card memory game implemented in Blazor WASM. https://kristofferstrube.github.io/FlipGame/
HTML
24
star
13

ActivityStreams

A .NET implementation of the Activity Streams vocabulary in the form of classes that can be serialized/deserialized using System.Text.Json
C#
24
star
14

Blazor.DOM

A Blazor wrapper for the DOM browser API.
C#
19
star
15

Blazor.WebIDL

A Blazor wrapper for types and interfaces used in and defined in the standard WebIDL specification.
C#
16
star
16

Blazor.Window

A Blazor wrapper for the Window interface from the HTML Specification.
C#
14
star
17

ActivityPubBotDotNet

An implementation of a ActivityPub bot that can communicate with Mastodon servers.
C#
14
star
18

Blazor.Popover

A Blazor wrapper for the part of the HTML API related to the popover attribute.
C#
14
star
19

DistributedStickyNotes

A distributed real-time sticky note application made in Blazor
C#
13
star
20

Blazor.Streams

A Blazor wrapper for the Streams browser API.
C#
12
star
21

StaticBlog.NET

A Static Blog Generator written in .NET which uses a Blazor WASM site to edit the content of the blog.
C#
12
star
22

Blazor.WebWorkers

A Blazor wrapper for the Web Workers part of the HTML API.
C#
12
star
23

Blazor.SVGAnimation

A Blazor wrapper for the SVG Animation browser API
C#
11
star
24

Blazor.Confetti

A small service that can make confetti in your Blazor application.
HTML
11
star
25

PWAPushNotification

This project is an example of how a browser can subscribe to PWA Push Notifications and how these notifications can then be pushed from ASP.NET Core.
C#
10
star
26

Blazor.Bubbles

A fullscreen Bubble animation made in Blazor Wasm
C#
9
star
27

DocumentSearching

A document based search library for .NET
C#
7
star
28

BlazorBackgroundAnimation

Test project that uses delaunay triangulation to make a background in Blazor.
HTML
6
star
29

Blazor.RadialContextMenu

A Radial Context Menu for Blazor WASM
C#
5
star
30

Blazor.Konva

Konva.js wrapper implementation for Blazor Wasm
C#
4
star
31

Blazor.DeviceOrientation

A Blazor wrapper for the browser Device Orientation API.
C#
4
star
32

AoC2021.NET6

My solutions for Advent of Code 2021. Written in C# using .NET 6's top-level statements and implicit usings with a focus on using new .NET 6 features and LINQ.
C#
4
star
33

SciPaWiz

The project uses the DBLP and OpenCitations APIs to construct a visual representation of how scientific papers in the field of Computer Science cite each other.
JavaScript
4
star
34

Blazor1brc

My attempt at The One Billion Row Challenge using Blazor WASM and the File System Access API.
C#
4
star
35

MovableComponentsBlazorExample

A sample of having multiple render fragments in a component and being able to move each between different simple layouts.
HTML
4
star
36

Blazor.CompressionStreams

A Blazor wrapper for the Compression Streams browser API.
C#
3
star
37

FollowTheLeader

A repository for a game made through K-JAM in the weekend 4-6 november 2022. The game is a multiplayer real-time game inspired by slither.io and Follow The Leader. It is made in Blazor with the use of SVGs.
HTML
3
star
38

Blazor.BanditAlgorithms

A repository for exploring and visualizing bandit algorithms
C#
2
star
39

GoodOrBadText

An example of ML.NET used for text-categorization directly in a Blazor WASM project
HTML
2
star
40

Blazor.CSSView

A Blazor wrapper for the CSSOM View Module browser API.
C#
2
star
41

Blazor.TreemapChart

A project that illustrates the use of Treemaps (rectangle area chart) in Blazor.
HTML
2
star
42

AoC2023

My Solutions for Advent of Code 2023
C#
2
star
43

Blazor.Relewise

A tool for interacting with the Relewise API in a no-code fashion using Blazor components.
HTML
2
star
44

Blazor.CSSFontLoading

A Blazor wrapper for the CSS Font Loading browser API.
C#
1
star
45

GravityBoxGame

A small maze game that demonstrates collision detection and estimated gravity in a SVG using Blazor WASM/WebAssembly. https://kristofferstrube.github.io/GravityBoxGame/
HTML
1
star
46

Blazor.WorkersBenchmarks

This is a project for comparing the performance of 3 different libraries that enable us to execute .NET code in the background of a Blazor WASM application using Web Workers.
HTML
1
star
47

BlazorSVGBugExample

A repo to reproduce SVG using bug in Blazor WASM
CSS
1
star
48

Blazor.MediaStreamRecording

A Blazor wrapper for the MediaStream Recording browser API.
C#
1
star