且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Blazor WebAssembly 中获取静态图像文件的文件路径列表?

更新时间:2023-02-20 19:21:07

我的第一个想法是 Directory.GetFiles() 不应该在 Blazor WebAssembly 上得到支持 - 你在文件系统上不被允许.
但是运行 System.IO.Directory.GetDirectories(".") 给出了这个输出:

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

您可以获取一些预先打包的文件和文件夹.但它们都没有包含 wwwroot 和您想要的内容.

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

因此您可以使用 Http.GetStreamAsync()Http.GetByteArrayAsync() 来获取图像文件的内容您已经知道/em>.没有直接的方法来扫描文件夹:一种安全功能.(我知道您可以将 ISS 配置为允许浏览"文件夹,但这违背了***实践并为您提供了 HTML 进行解析).

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

如果您确实想扫描文件夹,请构建 API 来执行此操作.您需要在服务器上运行它.

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