I'm building a simple image board with Blazor WASM. OnInitialize I want to get all file names in the "image" folder, located in wwwroots. Here is my code with two approaches. The second approach is commented out. Both are giving me the same error message.
public partial class Index
{
string str = "";
string[] allfiles = Directory.GetFiles(@"images", "*.*", SearchOption.AllDirectories);
protected override void OnInitialized()
{
foreach (string file in allfiles)
{
Console.WriteLine(file);
}
}
//DirectoryInfo d = new DirectoryInfo(@"images");
//FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files
//foreach (FileInfo file in Files)
//{
// Console.WriteLine(str = str file.Name);
//}
}
Does anyone know how to fix that? I feel so dumb, because I'm like "But there is the image folder.. Why doesn't it work?" It also doesn't work, if use @"/images" instead of @"images"
CodePudding user response:
Web Assembly runs in the same browser sandbox that JavaScript uses and so has many of the same limitations. The security context is the same, and Web Assembly cannot access the underlying hardware, operating system or file system outside of the sandbox.
Not sure if it will be useful for your application but you can get the static files inside wwwroot folder via http call to the server.
[Inject]
HttpClient Client { get; set; }
async Task GetPhoto()
{
var photo = await Client.GetByteArrayAsync("images/bgtest.png");
}