Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to acces an image/file in blazor @code section

Looking into opening an image on a blazor page’s @code section, but I keep getting the following error.

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\img\Account\Pamphlet Design.png'

Here is the code that I have tried among other:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var path = Path.Combine(Environment.ContentRootPath, @"/img/Account/Pamphlet Design.png");

var plampletBitmap = new Bitmap(path); // Error Here

>Solution :

I’m not sure that Blazor can access an ASP.NET content root path, maybe in server mode, almost certainly not in web assembly mode.

Normally to access images you can create a /images folder under the Blazor Client’s wwwroot and load the images from there via: images/filename. I just compute the path in the model and bind it directly to an <img src="@Model.ImagePath">

For Blazor server you could try Path.Combine(Environment.ContentRootPath, "wwwroot", "img/Account", "Pamphlet Design.png"); I think even in Blazor server it sandboxes file access, though the leading slash in "/img" might also just be the problem if not.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading