KristofferStrube.Blazor.FileSystemAccess 4.0.0

Prefix Reserved
dotnet add package KristofferStrube.Blazor.FileSystemAccess --version 4.0.0
                    
NuGet\Install-Package KristofferStrube.Blazor.FileSystemAccess -Version 4.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KristofferStrube.Blazor.FileSystemAccess" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KristofferStrube.Blazor.FileSystemAccess" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="KristofferStrube.Blazor.FileSystemAccess" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KristofferStrube.Blazor.FileSystemAccess --version 4.0.0
                    
#r "nuget: KristofferStrube.Blazor.FileSystemAccess, 4.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package KristofferStrube.Blazor.FileSystemAccess@4.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=KristofferStrube.Blazor.FileSystemAccess&version=4.0.0
                    
Install as a Cake Addin
#tool nuget:?package=KristofferStrube.Blazor.FileSystemAccess&version=4.0.0
                    
Install as a Cake Tool

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 notably 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 browser being used.

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
        {
            OpenFilePickerOptions options = new()
                {
                    Multiple = false,
                    StartIn = WellKnownDirectory.Downloads
                };
            var fileHandles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);
            fileHandle = fileHandles.Single();
        }
        catch (JSException ex)
        {
            // Handle Exception or cancellation of File Access prompt
            Console.WriteLine(ex);
        }
        finally
        {
            if (fileHandle is not null)
            {
                var file = await fileHandle.GetFileAsync();
                Text = await file.TextAsync();
                StateHasChanged();
            }
        }
    }
}

Upgrade to version 4

Version 4 of this library made several breaking changes. To make the transition from version 3 or earlier to this version easier, I have made a small guide below for how you can upgrade.

File/Directory picker options

We changed the method signatures for opening the directory and file picker dialogs to make them simpler to use.

To migrate replace the following:

  • OpenFilePickerOptionsStartInWellKnownDirectory or OpenFilePickerOptionsStartInFileSystemHandle with OpenFilePickerOptions.
  • SaveFilePickerOptionsStartInWellKnownDirectory or SaveFilePickerOptionsStartInFileSystemHandle with SaveFilePickerOptions.
  • DirectoryPickerOptionsStartInWellKnownDirectory or DirectoryPickerOptionsStartInFileSystemHandle with DirectoryPickerOptions.

As an example, if you had the following before:

OpenFilePickerOptionsStartInWellKnownDirectory options = new() 
    {
        Multiple = false,
        StartIn = WellKnownDirectory.Downloads
    };
var fileHandles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);

Update it as follows:

OpenFilePickerOptions options = new() 
    {
        Multiple = false,
        StartIn = WellKnownDirectory.Downloads
    };
var fileHandles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);

Removed FileSystemAccessOptions

We removed the FileSystemAccessOptions parameter and all methods that previously accepted it as it duplicated functionality that could be achieved in other ways.

Instead of using them, you need to configure an importmap if you want to define custom paths for loading the helper modules from this library.

Blazor also has a native ImportMap component that plays well with fingerprinted resources.

Blazor.FileSystem removed FileSystemDirectoryHandle.ValuesAsync

The ValuesAsync method was removed from Blazor.FileSystem, and instead you should now use the ValuesAsync extension method available from Blazor.WebIDL.

This change was made to make it easier to handle memory safely.

So if you had the following before:

var values = await directoryHandle.ValuesAsync();
for (int i = 0; i < values.Count(); i++)
{
    var value = values[i];
    string name = await value.GetNameAsync();
    FileSystemHandleKind kind = await value.GetKindAsync();
    Console.WriteLine($"'{name}' is a {kind}");
    await value.DisposeAsync();
}

Then you should change it to the following:

await using var valuesIterator = await directoryHandle.ValuesAsync(disposePreviousValueWhenMovingToNextValue: true);
await foreach (FileSystemHandle value in valuesIterator)
{
    string name = await value.GetNameAsync();
    FileSystemHandleKind kind = await value.GetKindAsync();
    Console.WriteLine($"'{name}' is a {kind}");
}

In the above example, we pass true for the disposePreviousValueWhenMovingToNextValue parameter, which is also the default. This means that it will dispose of each handle once it iterates past it. If you need the handles after iterating, you can pass false for this parameter instead.

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 amounts of data in Blazor Server is not supported.

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

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

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on KristofferStrube.Blazor.FileSystemAccess:

Package Downloads
Mythetech.Framework.WebAssembly

WebAssembly-specific components for building blazor applications

Mythetech.Components.WebAssembly

WebAssembly-specific components for building blazor applications

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on KristofferStrube.Blazor.FileSystemAccess:

Repository Stars
codemonkey85/PKMDS-Blazor
PKMDS: Pokémon Save Editor for Web (no support for game hacks)
Version Downloads Last Updated
4.0.0 427 4/14/2026
3.3.0 14,360 4/8/2025
3.2.2 33,854 10/19/2023
3.2.1 1,381 10/5/2023
3.2.0 7,882 3/16/2023
3.1.0 1,369 2/15/2023
3.0.0 1,623 1/11/2023
2.1.0 2,103 11/18/2022
2.0.0 709 11/10/2022
1.2.1 1,292 10/10/2022
1.2.0 598 10/7/2022
1.1.0 1,060 8/19/2022
1.0.1 705 7/12/2022
1.0.0 639 6/28/2022
0.2.0 656 6/14/2022
0.1.0 946 4/26/2022