How to upload pictrue or image file from directory where client saved

MIPAKTEH_1 545 Reputation points
2025-03-10T15:20:11.26+00:00

@page "/view-page"

<h3>Blank View Page</h3>

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

{

<div class="tshirt-section">

    <img src="@tshirtItems[i].ImagePath" alt="T-Shirt Image" />

    <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 class TShirt

{

    public string ImagePath { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set; }

}
```}

Blazor Training
Blazor Training
Blazor: A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.Training: Instruction to develop new skills.
21 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 72,276 Reputation points
    2025-03-10T16:11:14.1933333+00:00
    0 comments No comments

  2. Ping Ni-MSFT 4,890 Reputation points Microsoft External Staff
    2025-03-12T06:40:45.5833333+00:00

    Hi @MIPAKTEH_1,

    Here is a whole working demo you could follow:

    @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 {
        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; }
        }
    }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Rena

    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.