Convert File To byte[] in Blazor WA

Viewed 180

I used this article to download files from Blazor Server and it works fine.

But when I used it with Blazor WA I faced two problems in converting the file into byte[]

first one that when I used the code string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "files\file.rar"); to return the file path it returns "/" instead of the Solution Directory.

So I tried to put a static path into the following Code `byte[] fileBytes = System.IO.File.ReadAllBytes(@"D:\XXX\wwwroot\files\file.rar");

but the File.ReadAllBytes puts "/" before the file path and returns the following error

"Unhandled exception rendering component: Could not find file '/D:\XXX\wwwroot\files\file.rar'. System.IO.FileNotFoundException: Could not find file '/D:\XXX\wwwroot\files\file.rar'. File name: '/D:\XXX\wwwroot\files\file.rar"

So the main point that I want to prevent Blazor WA from putting "/" before the file path into the function (File.ReadAllBytes).

And the secondary one is that I want to retrieve the Project folder path using "Directory.GetCurrentDirectory()" or any other method.

0 Answers
Related