New to Telerik UI for ASP.NET MVCStart a free 30-day trial

Upload Files to a Database

Environment

ProductTelerik UI for ASP.NET MVC Upload
Progress Telerik UI for ASP.NET MVC version2024.4.1112

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.

The example uses an Upload nested in a Grid 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
        @(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#
        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

See Also