Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor read directory

How do you read a content directory into Blazor.

I tried

Path.Combine(Directory.GetCurrentDirectory(), "..", "_posts");

But this throws an error.

Uncaught (in promise) Error: System.IO.DirectoryNotFoundException: Could not find a part of the path '/_posts'.
like image 800
Ivan Ivanov Avatar asked Apr 27 '26 13:04

Ivan Ivanov


2 Answers

The answer depends on why you want to read the directory

  • If you want to interact with the user's file system, then you need to use the HTML standard for accessing the file system. A blazor wrapper is provided at https://github.com/Tewr/BlazorFileReader
  • If you want to retrieve files from your server, there are multiple approaches based on how you want to use it. You could set up an api, an mvc instance, or just serve static files. They'll all be relative to your server though, not the current directory.
  • If you want store data client-side, Flores made a good suggestion to use https://github.com/BlazorExtensions/Storage
like image 183
farlee2121 Avatar answered Apr 29 '26 22:04

farlee2121


Clientside Blazor is running in the same sandbox that javascript is running in. Which means your code has no way of accessing the local filesystem.

Maybe localStorage could be an alternative for you? There is a Blazor package to use it here: BlazorExtensions/Storage

like image 20
Flores Avatar answered Apr 29 '26 23:04

Flores



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!