c# - FileStream could not find a part of the path - Stack Overflow

admin2025-04-19  3

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.

Share Improve this question edited Feb 6 at 7:23 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Jan 28 at 18:54 VemeVeme 3841 silver badge12 bronze badges 3
  • is there any other content import/access beside that file ? – Hicham O'Sfh Commented Jan 29 at 1:50
  • created empty project with only that import for tests – Veme Commented Jan 29 at 9:03
  • WebAssembly runs on the browser. There are no file paths on the browser. If you want to load an image from the server you'll have to get it with HttpClient or use an 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
Add a comment  | 

3 Answers 3

Reset to default 3

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:

转载请注明原文地址:http://anycun.com/QandA/1745001026a90337.html