File Processing Approaches
- 2 minutes to read
There are different ways to integrate the Upload Control into your ASP.NET MVC application depending on how you want to handle processing of uploaded files. This document describes the available approaches and provides information on scenarios for which these approaches are best suited.
Custom Action
The Upload Control allows you to process uploaded files in a custom controller action. In the implementation of the custom action, you can access a collection of uploaded items using the static UploadControlExtension.GetUploadedFiles method.
public ActionResult FileUpload() {
UploadedFile[] files = UploadControlExtension.GetUploadedFiles("uploadControl");
// Your custom logic to process uploaded files.
return null;
}
Alternatively, a collection of uploaded files can be mapped to the action’s parameter through model binding.
public ActionResult FileUpload([ModelBinder(typeof(DevExpressEditorsBinder))] IEnumerable<UploadedFile> UploadControl) {
List<UploadedFile> uploadedFilesList = UploadControl.ToList();
// Your custom logic to process uploaded files.
return null;
}
In the view code, specify your custom action as a callback handler using the UploadControlSettings.CallbackRouteValues property.
@using(Html.BeginForm()) {
...
@Html.DevExpress().UploadControl(
settings => {
settings.Name = "uploadControl";
settings.CallbackRouteValues = new { Controller = "Home", Action = "FileUpload" };
settings.ShowProgressPanel = true;
settings.ShowUploadButton = true;
}
).GetHtml()
...
}
This approach is best suited for applications requiring you to perform custom file processing involving the following tasks:
- Custom file validation.
- Custom processing of file contents.
- Saving uploaded files to a custom storage.
Partial View
The Upload Control allows you to process uploaded files in an event handler within the view code. To use this approach, declare the Upload Control in a partial view. The code sample below demonstrates how to process uploaded files in the UploadControlSettings.FileUploadComplete event handler.
@using (Html.BeginForm()) {
...
@Html.DevExpress().UploadControl(
settings => {
settings.Name = "uploadControl";
settings.CallbackRouteValues = new { Controller = "Home", Action = "UploadControlPartial" };
settings.FileUploadComplete = (s, e) => {
// Your custom logic to process uploaded files.
};
settings.ShowProgressPanel = true;
settings.ShowUploadButton = true;
}
).GetHtml()
...
}
In this case, the action handling callbacks just returns the partial view.
public ActionResult UploadControlPartial() {
return PartialView("UploadControlPartial");
}
The described approach is preferable when there is little or no custom logic involved in file processing; the Upload Control uses the standard validation mechanism and files are saved to one of the standard storages.