且构网

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

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

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

我的第一个想法是Blazor WebAssembly不应该支持Directory.GetFiles()-文件系统上不允许您使用.
但是运行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()来获取图像文件的内容,而您已经知道该文件的名称.没有直接扫描文件夹的方法:一种安全功能. (我知道您可以将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.