'Blazor Wasm Upload Files Hangs

When I try to load a binary file to server the client refresh the page in a strange way...

Setup: Asp.Net 6 with Client (Blazor wasm) and Server (Asp.Net)

Client code:

<InputFile OnChange="@OnInputFileChange" />


private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
    var file = e.File;
    using var content = new MultipartFormDataContent();
    content.Add(new StreamContent(file.OpenReadStream()), "file", file.Name);

    try
    {
        var response = await Http.PostAsync("/ModuleManager", content);

        var value = await response.Content.ReadAsStringAsync(); // Never came here
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
    }    
}

Server Code

    [HttpPost]
    public async Task<IActionResult> Index([FromForm] IFormFile file)
    {
        // Check folder name
        using MemoryStream ms = new();
        await file.OpenReadStream().CopyToAsync(ms);
        var bytes = ms.ToArray();
        var folderName = file.FileName.Substring(0, file.FileName.IndexOf('.'));

        // Create server path
        string path = Path.Combine(env.WebRootPath, "_content", folderName);
        Directory.CreateDirectory(path);

        // Save resources
        LoadResources(bytes, path);

        return Created($"/_content/{folderName}", folderName);
    }

The file is loaded but the page reloads and the commented line on client method never gets hit, looking the Browser DevTools I can see this in Console:

enter image description here

And this in Network:

enter image description here

Seems like Blazor is restarting the cache serveral times and refresh the page, like a memory leak or something like that? The "catch" section is not reached either.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source