Using FileStream
with Blazor results in Could not find a part of the path
error while trying to load file. I tried using Path.Combine
instead of direct path, but it didn't help
FileStream stream = new FileStream("Content/Testing/testfile.jpg", FileMode.Open, FileAccess.Read);
If it's relevant, I'm trying to integrate Spine Skeletal Animation C#/MonoGame runtime with KNI MonoGame extension. Everything works fine except one atlas file that is loaded via FileStream and it's hard for me to figure out where exactly lays the issue.
Using FileStream
with Blazor results in Could not find a part of the path
error while trying to load file. I tried using Path.Combine
instead of direct path, but it didn't help
FileStream stream = new FileStream("Content/Testing/testfile.jpg", FileMode.Open, FileAccess.Read);
If it's relevant, I'm trying to integrate Spine Skeletal Animation C#/MonoGame runtime with KNI MonoGame extension. Everything works fine except one atlas file that is loaded via FileStream and it's hard for me to figure out where exactly lays the issue.
Your error messages show that you are on Blazor Webassembly.
That means that you do not have access to the (server) file system.
In the browser a FileStream
can only be opened on the memfs filesystem that is part of Blazor. But that does not hold any of your Content/**
files.
The solution is to use HttpClient.GetStreamAsync(uri)
and maybe copy that to a MemoryStream first.
When you say the file was loaded by FileStream, that sounds like it wasn't part of the build, but was added to the site after deployment.
If so, then I had the same issue last week. Turns out that ASP.NET Core will only serve files it knows about, ie ones that were in the project and set to output in the deploy. Otherwise you get a 404 unless you specifically tell it to allow these files.
The short answer is to add the following to your server project's Program.cs
file...
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
RequestPath = "/images"
});
app.MapStaticAssets();
For more details, please read my blog post which gives the background.
Agree with Henk Holterman. And you might be putting the image file in the wwwroot
folder, then to access them in the @code
block, you can use the httpclient to send the request and fetch the image as a byte array, after that convert to base64 string and display it.
Refer to the following sample code:
@page "/fetch-image"
@inject HttpClient Http
<h3>Fetch Image from Static Files</h3>
@if (imageSrc != null)
{
<img src="@imageSrc" alt="Sample Image" style="width:200px;height:200px;" />
}
else
{
<p>Loading image...</p>
}
@code {
private string imageSrc;
protected override async Task OnInitializedAsync()
{
// Path to the image in the wwwroot folder
var imagePath = "sample-data/Image1.jpg";
// Fetch the image as a byte array
var imageBytes = await Http.GetByteArrayAsync(imagePath);
// Convert the byte array to a base64 string
var base64 = Convert.ToBase64String(imageBytes);
// Set the image source
imageSrc = $"data:image/jpeg;base64,{base64}";
}
}
The output as below:
img
tag with the correct source. You can't access local files on the client machine because the browser itself prevents it – Panagiotis Kanavos Commented Jan 31 at 14:38