To upload a file in Blazor server-side application, you can make use of the built-in InputFile
component provided in the Microsoft.AspNetCore.Components.Forms
namespace. The InputFile
component contains a OnChange
event that is triggered when the user selects a file in the file dialog window. This event can be handled to receive the selected file stream, which can then be saved to the server.
Here is an example code snippet that demonstrates how to upload a file and save it on the server:
main.cs925 chars38 lines
In the above example, the InputFile
component is used to create a file input field on the UI. When a file is selected, the HandleSelectedFile
method is called, which receives the selected file stream from the OnChange
event. The file is then saved using the code block inside the method.
Note that in order to use the WebHostEnvironment
service, you need to add it as a component service in the Startup.cs
file of your application. You can add it to the ConfigureServices
method as follows:
main.cs149 chars6 lines
This will make the WebHostEnvironment
service available for dependency injection in your Blazor components.
gistlibby LogSnag