If I upload image to my web App with ASP.NET CORE and Blazor Assembly StandAlone App to the folder Uploads My question is How to show all images to the new page using coding above

MIPAKTEH_1 605 Reputation points
2025-03-23T10:15:10.3+00:00

@page "/view-page"

<h3>Blank View Page</h3>

@for (int i = 0; i < tshirtItems.Count; i++)

{

var index = i;

<div class="tshirt-section">

    <img src="@tshirtItems[i].ImagePath" alt="T-Shirt Image" style="max-width: 150px; max-height: 150px;" />

    <div>

        <label for="tshirtImage">Upload Image:</label>

        <InputFile OnChange="e => UploadImage(e, index)" />

    </div>

    <div>

        <label for="tshirtName">T-Shirt Name:</label>

        <input type="text" id="tshirtName" @bind="tshirtItems[i].Name" />

    </div>

    <div>

        <label for="tshirtPrice">Price:</label>

        <input type="number" id="tshirtPrice" @bind="tshirtItems[i].Price" />

    </div>

</div>
```}

@code {

```powershell
private List<TShirt> tshirtItems = new List<TShirt>

{

    new TShirt { ImagePath = "", Name = "Shirt 1", Price = 0 },

    new TShirt { ImagePath = "", Name = "Shirt 2", Price = 0 },

    new TShirt { ImagePath = "", Name = "Shirt 3", Price = 0 },

    new TShirt { ImagePath = "", Name = "Shirt 4", Price = 0 },

    new TShirt { ImagePath = "", Name = "Shirt 5", Price = 0 },

    new TShirt { ImagePath = "", Name = "Shirt 6", Price = 0 }

};

private async Task UploadImage(InputFileChangeEventArgs e, int index)

{

    var file = e.File; // Get the uploaded file

    if (file != null)

    {

        var buffer = new byte[file.Size];

        await file.OpenReadStream().ReadAsync(buffer);

        // Convert to Base64

        var base64Image = Convert.ToBase64String(buffer);

        tshirtItems[index].ImagePath = $"data:image/png;base64,{base64Image}";

    }

}

private class TShirt

{

    public string ImagePath { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set; }

}
```}
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 79,106 Reputation points Volunteer Moderator
    2025-03-23T16:03:29.3+00:00

    Is your components render mode interactive, so the the onchange event fires?


  2. SurferOnWww 4,821 Reputation points
    2025-03-24T05:58:47.1366667+00:00

    Do you want show the images uploaded to "web App with ASP.NET CORE" on the "new page" of "Blazor Assembly StandAlone App"?

    If yes:

    (1) Add controller to your "web App with ASP.NET CORE" app which can download the uploaded image file, and

    (2) Add the img tag to the "new page" and set src attribute to the url of the controller.


  3. AgaveJoe 30,206 Reputation points
    2025-04-07T10:15:48.0633333+00:00

    I think SurferOnWww right but I very newest blank to start it.If have some example..

    The official Blazor documentation is the recommended starting point for your learning, and it includes file upload examples.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-9.0

    0 comments No comments

  4. Danny Nguyen (WICLOUD CORPORATION) 800 Reputation points Microsoft External Staff
    2025-08-13T03:49:11.3966667+00:00

    Hi @MIPAKTEH_1 ,

    A Blazor WebAssembly Standalone app runs entirely in the browser — it cannot write files directly to the server’s file system. To persist uploads, you’ll need a backend endpoint (Web API, Blazor Server, etc.) to receive and save the file.

    Here’s the general Upload → Save → Display pattern you can follow:


    1. Client Side (Blazor WebAssembly or Blazor Server)

    • Let the user choose a file using the <InputFile> component.
    • When a file is picked, send it to the server using HttpClient with MultipartFormDataContent.
    • After the upload, either:
      • Add the new image URL to the current list, or
      • Request a fresh list of images from the server.

    Example:

    <InputFile OnChange="HandleUpload" />
     
    @code {
        private List<string> images = new();
     
        private async Task HandleUpload(InputFileChangeEventArgs e)
        {
            var file = e.File;
            using var content = new MultipartFormDataContent();
            content.Add(new StreamContent(file.OpenReadStream(10_000_000)), "file", file.Name);
     
            var response = await Http.PostAsync("api/uploads", content);
            if (response.IsSuccessStatusCode)
            {
                images = await Http.GetFromJsonAsync<List<string>>("api/uploads");
            }
        }
    }
    

    WebAssembly runs in the browser sandbox — uploading to the server requires a POST to your backend.


    2. Server Side (ASP.NET Core Web API or Blazor Server)

    • Accept the file as IFormFile in a controller.
    • Validate the type, size, and MIME type.
    • Save it to wwwroot/Uploads so it’s accessible as a static file.
    • Return the file’s public URL.

    Example:

    [HttpPost]
    public async Task<IActionResult> Upload(IFormFile file)
    {
        if (file == null || file.Length == 0)
            return BadRequest("No file uploaded");
     
        var uploadsPath = Path.Combine(_env.WebRootPath, "Uploads");
        Directory.CreateDirectory(uploadsPath);
     
        var fileName = Path.GetRandomFileName() + Path.GetExtension(file.FileName);
        var filePath = Path.Combine(uploadsPath, fileName);
     
        using var stream = System.IO.File.Create(filePath);
        await file.CopyToAsync(stream);
     
        return Ok($"/Uploads/{fileName}");
    }
    

    3. Listing Uploaded Images

    • Create a GET endpoint to return all image URLs from the Uploads folder.

    Example:

    [HttpGet]
    public IActionResult List()
    {
        var uploadsPath = Path.Combine(_env.WebRootPath, "Uploads");
        if (!Directory.Exists(uploadsPath))
            return Ok(Array.Empty<string>());
     
        var files = Directory.GetFiles(uploadsPath)
                             .Select(f => "/Uploads/" + Path.GetFileName(f));
        return Ok(files);
    }
    

    4. Displaying the Images in Blazor

    Example:

    @foreach (var img in images)
    {
    <img src="@img" style="max-width:150px;" />
    }
    

    I suggest checking these out for better understanding of the pieces


    Hope this helps. Please reach out if you need any clarification.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.