Save file from somebody to my web Blazor.I try browse in internet but not found how to start write the code.Let said put that file int TextBox and then need to upload,,How

MIPAKTEH_1 365 Reputation points
2024-06-18T10:04:36.0633333+00:00
@page "/"
@rendermode InteractiveServer
@inject NavigationManager Navigation

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.
<p></p>
@foreach (var image in images)
{
    <div style="margin:10px 20px; float:left;">
        <img src=@($"/Images/{image}") asp-append-version="true" : style="width=100px;height:100px" @onclick="() => NavigateToNewPage()" />
        <div class='e-input' style="margin-top:5px; width:120px;">
            <input class='e-input' style="width:100%" Placeholder='' type='text' />
        </div>
    </div>
}
@code {
    // Define the list of image file names
    private readonly List<string> images = new()
    {
        "Car2.jpg"
    };

    private void NavigateToNewPage()
    {
        Navigation.NavigateTo("/PageCar2");
    }

}

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
0 comments No comments
{count} votes

Accepted answer
  1. Brando Zhang-MSFT 3,706 Reputation points Microsoft Vendor
    2024-06-18T10:54:34.3566667+00:00

    Hi @MIPAKTEH_1,

    If you want to upload file inside the blazor web app , you could use InputFile taghelper and bind it with a change method to get the file and save it at server side.

    More details, you could refer to below example:

    1.Firstly you could create a UploadedFiles folder inside the root path of your blazor app.

    2.You could use below codes to upload file:

    @page "/"
    @rendermode InteractiveServer
    @inject NavigationManager Navigation
    <PageTitle>Home</PageTitle>
    <h1>Hello, world!</h1>
    Welcome to your new app.
     
    <InputFile OnChange="HandleFileSelected"   />
    @if (!string.IsNullOrEmpty(selectedFileName))
    {
        <p>Selected file: @selectedFileName</p>
     
    }
    @code {
        private readonly List<string> images = new()
        {
            "Car2.jpg"
        };
        private void NavigateToNewPage()
        {
            Navigation.NavigateTo("/PageCar2");
        }
        private string selectedFileName;
      
        private async void HandleFileSelected(InputFileChangeEventArgs e)
        {
            var file = e.File;
            selectedFileName = file.Name;
          
            var folderPath = Path.Combine(Environment.CurrentDirectory, "UploadedFiles");
            // Ensure the folder exists
            Directory.CreateDirectory(folderPath);
            // Define the complete file path
            var filePath = Path.Combine(folderPath, file.Name);
            // Save the file to the server
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                await file.OpenReadStream().CopyToAsync(fileStream);
            }
        }
    }
    

    It will show a button named choose file, if you want to upload file, then after selecting the file, it will save at the server's folder.

    Like below:

    User's image

    Result:

    User's image


    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 28,536 Reputation points
    2024-06-18T10:30:38.97+00:00

    If I understand correctly, you are asking how to upload a file in Blazor. See the official documentation.

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

    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.