You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Form Integration in Blazor File Upload Component
11
11
12
-
The Syncfusion Blazor File Upload component can be integrated with Blazor's `EditForm` and `DataForm`to build robust forms that include file-handling capabilities. This allows you to associate file uploads with a model and trigger validation.
12
+
The Syncfusion Blazor File Upload component seamlessly integrates with Blazor's [EditForm](https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/?view=aspnetcore-9.0) and the Syncfusion [DataForm](https://blazor.syncfusion.com/documentation/data-form/getting-started-with-web-app), enabling you to build robust forms with file upload functionality. This integration associates the uploaded file information with a data model, leveraging the form's built-in validation.
13
13
14
-
In a form, file validation is triggered by populating a model property when a file is selected and clearing it when removed. The `FileSelected` and `OnRemove` events are used for this purpose. When the form is submitted, the file can be uploaded manually to a server-side endpoint.
14
+
When a file is selected, its information is added to the model property bound to the component. Upon form submission, the entire model, including the list of selected files, is passed to the submit event handler.
15
15
16
16
## File Upload with EditForm Integration
17
17
18
-
The File Upload component can be integrated into a Blazor `EditForm`to manage file uploads as part of your data entry process.
18
+
Integrating the File Upload component into a Blazor `EditForm`streamlines data entry by including file management directly within the form.
19
19
20
-
To validate the file input, a model property is updated using the `FileSelected`and `OnRemove` events. The file upload is initiated only when the form is valid by calling the `UploadAsync` method in the `OnValidSubmit` event handler.
20
+
Validation for the file input is achieved by binding the component to a model property. The [ValueChange](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Inputs.UploaderEvents.html#Syncfusion_Blazor_Inputs_UploaderEvents_ValueChange)and [OnRemove](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Inputs.UploaderEvents.html#Syncfusion_Blazor_Inputs_UploaderEvents_OnRemove) events are used to update this property with the current list of files. Within these events, it is crucial to call `EditContext.NotifyFieldChanged` to trigger the form's validation logic immediately after the file list changes.
21
21
22
-
### Uploading to a Server-Side API (Blazor WASM and Server)
23
-
24
-
This example shows how to configure the File Upload component to send files to a server-side API endpoint upon form submission. This approach is compatible with both Blazor WebAssembly and Blazor Server applications.
22
+
When the form is successfully submitted, the `OnValidSubmit` event handler receives the `EditContext`, which contains the complete form model.
public List<UploaderUploadedFiles> files { get; set; }
50
-
}
51
-
52
-
public Employee employee = new Employee();
53
-
54
-
public void OnSelect(SelectedEventArgs args)
55
-
{
56
-
this.employee.files = new List<UploaderUploadedFiles> { new UploaderUploadedFiles { Name = args.FilesData[0].Name, Type = args.FilesData[0].Type, Size = args.FilesData[0].Size } };
57
-
}
58
-
59
-
public void OnRemove()
60
-
{
61
-
this.employee.files = null;
62
-
}
63
-
64
-
public async Task HandleValidSubmit()
65
-
{
66
-
// Upload the selected file manually only when the form is valid
67
-
await this.UploadObj.UploadAsync();
68
-
}
69
-
}
70
-
```
71
-
72
-
### Saving Files Directly to the File System (Blazor Server Only)
73
-
74
-
In a Blazor Server application, you can save uploaded files directly to the server's file system. The following example demonstrates how to use the `ValueChange` event to read the file stream and save it to a path.
75
-
76
-
> This method writes files to the server's local file system and is only supported in Blazor Server applications. It will not work in a Blazor WebAssembly environment.
this.employee.files = new List<UploaderUploadedFiles> { new UploaderUploadedFiles { Name = args.FilesData[0].Name, Type = args.FilesData[0].Type, Size = args.FilesData[0].Size } };

137
83
138
84
## File Upload with DataForm Integration
139
85
140
-
The File Upload component can also be integrated into a Syncfusion `DataForm`, providing a streamlined way to create forms from a model. The example below shows how to save an uploaded file directly to the file system in a Blazor Server application when the form is submitted.
86
+
The File Upload component can also be integrated into a Syncfusion `DataForm` to automatically build a form from a model that includes file upload capabilities.
87
+
88
+
When the `DataForm` is submitted, the [OnSubmit](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.DataForm.SfDataForm.html#Syncfusion_Blazor_DataForm_SfDataForm_OnSubmit) event handler receives the `EditContext`. The `EditContext.Model` property contains the complete form data, including the list of `FileInfo` objects from the File Upload component. This allows you to access and process the file information as part of the form's submission logic.
141
89
142
-
> This file-saving approach is only compatible with Blazor Server applications.
this.UserModel.files = new List<UploaderUploadedFiles> { new UploaderUploadedFiles { Name = args.FilesData[0].Name, Type = args.FilesData[0].Type, Size = args.FilesData[0].Size } };
131
+
var currentFiles = await UploaderObj.GetFilesDataAsync();
132
+
var fileToRemove = args.FilesData.FirstOrDefault();
0 commit comments