How can I get a list of file paths for static image files in Blazor WebAssembly?
Asked Answered
B

2

6

I'm trying to get a list of the static files inside of my wwwroot folder in a Blazor WebAssembly project. This is what I've tried so far:

string[] images = Directory.GetFiles(@"wwwroot\images", "*.jpg");

I get this error:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  Unhandled exception rendering component: Could not find a part of the path '/wwwroot\images'.

System.IO.DirectoryNotFoundException: Could not find a part of the path '/wwwroot\images'.

What am I doing wrong? Thank you.

Breena answered 27/6, 2020 at 21:51 Comment(3)
Does this answer your question? What is the equivalent of Server.MapPath in ASP.NET Core?Flinders
Unfortunately, no. This only applies to Blazor Server- I am using Blazor WebAssembly.Breena
I don't think you can run this code in Blazor WebAssembly App. I know that if you want to read the content of a json file located in the wwwroot folder, you should use HTTP calls. But to retrieve the names of the files can be only done, I guess, using JSInterop. Try JSInterop, perhaps you'd succeed.Imaret
S
5

My first thought was that Directory.GetFiles() shouldn't be supported on Blazor WebAssembly - you are not allowed on the filesystem.
But running System.IO.Directory.GetDirectories(".") gives this output:

./tmp
./home
./dev
./proc
./zoneinfo

which are some pre-packaged files&folders you can get at. But none of them contain wwwroot and the contents you're after.

So you can use Http.GetStreamAsync() or Http.GetByteArrayAsync() to get the contents of an image file you already know the name of. There is no direct way to scan a folder: a security feature. (I know you can configure ISS to allow 'browsing' a folder but that goes against best practices and gives you HTML to parse).

If you do want to scan a folder, build an API to do so. You need to run that on the server.

Sensualism answered 28/6, 2020 at 0:10 Comment(4)
So if you're running a Blazor wasm standalone (not hosted) app, there's no way to enumerate the content-root or web-root paths?Swung
No, not that I know of. And that is right, there is no web-root path after launch. That is a server thing. Ask an API when you need something.Sensualism
I am creating a static blog with blazor wasm and the only way that worked for me was to construct a Json metadata file with a prebuild script and requesting it with the HttpClient in order to keep track of file count, dates and other metadata. hopefully this will be easier in the future.Swung
It is a little easier now, see github.com/KristofferStrube/Blazor.FileSystemAccessSensualism
P
-2

Please get "images" directory path like below:

string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images");
string[] images = Directory.GetFiles(folderPath, "*.jpg");

Hope it will work.

Pansy answered 3/3, 2022 at 13:6 Comment(1)
Has this solution been tested? First of all, it stands in contradiction to the previous answer. Furthermore, also in 2022 it does not seem to work.Vshaped

© 2022 - 2024 — McMap. All rights reserved.