# Upload Files to a Database

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Upload</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>2024.4.1112</td>
 </tr>
</table>

## Description

How can I upload files to a database using the Upload component?

## Solution

Refer to the following example of how [upload files to a database](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/UploadFilesToDataBase).

The example uses an Upload nested in a [Grid](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview) component. The Grid visualizes information about the files that are uploaded and saved in the database.

1. In the `Async` configuration of the Upload, set the `Save` action as the end point that will receive the files uploaded by the component.

   ```Razor HtmlHelper
       @(Html.Kendo().Upload()
           .Name("files")
           .Async(a => a.Save("Save", "Home"))
           .Events(e => e.Success("onUploadSuccess"))
           .ShowFileList(false) // Hide the file list as we're displaying uploaded files in the Grid
       )
   ```

2. Implement your files saving logic in the `Save` action.

   ```C# HomeController
       public ActionResult Save(IEnumerable<HttpPostedFileBase> files)
       {
           if (files != null)
           {
               UserFilesEntities db = new UserFilesEntities();

               foreach (var file in files)
               {
                   db.UserFile.Add(new UserFile()
                   {
                       Name = Path.GetFileName(file.FileName),
                       Data = GetFilesBytes(file)
                   });
               }

               db.SaveChanges();
           }

           // Return an empty string to signify success
           return Content("");
       }
   ```

## More ASP.NET MVC Upload Resources

* [ASP.NET MVC Upload Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/upload/overview)

* [ASP.NET MVC Upload Demos](https://demos.telerik.com/aspnet-mvc/upload/index)

* [ASP.NET MVC Upload Product Page](https://www.telerik.com/aspnet-mvc/upload)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Upload for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/upload)

* [Server-Side API Reference of the Upload for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/upload)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
