In this article I will explain with an example, how to upload MP4 Video files, save (insert) to database table, retrieve (display) files from database table and play the files with Live Streaming from database table in ASP.Net MVC Razor.
Files will be uploaded and then will be saved (inserted) to database table. The saved (inserted) files will be retrieved and displayed in HTML Grid (Table)  and then using FlowPlayer plugin (a Flash based video player), the files will be played with Live Streaming from database table in ASP.Net MVC Razor.
 
 
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net MVC
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file

 
 
Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;

 
 
Model
The following Model class consists of the following properties needed for inserting and populating the records of the Files from database.
public class FileModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ContentType { get; set; }
    public byte[] Data { get; set; }
}

 
 
Controller
The Controller consists of three Action methods.
Action method for handling GET operation
Inside this Action method, the GetFiles method is called.
Inside the GetFiles method, the records from the Files table are fetched using DataReader and generic list of FileModel class objects is populated.
Finally the generic list of FileModel class objects is returned to the View.
 
Action method for handling POST operation for uploading Files
This Action method gets called when a File is selected and the Upload Button is clicked, and it gets the uploaded file in the HttpPostedFileBase parameter.
Note: In case the HttpPostedFileBase parameter is appearing NULL, then please refer the article, ASP.Net MVC: HttpPostedFileBase always returns NULL.
 
The uploaded file is converted to an Array of Bytes using BinaryReader class and finally is inserted into the database table.
After successful insert of the File, the GetFiles method is called and the generic list of FileModel class objects is returned to the View.
 
Action method for handling GET operation for downloading Files
When the Video Player inside the HTML Table (Grid) requests the Video File to be streamed, then the ID of the particular file is sent to this Action method and using the File ID, the File’s binary data is fetched from the database.
Note: The following Action method performs File Download and hence the return type is set to FileResult.
 
Finally the File is downloaded using the File function.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View(GetFiles());
    }
 
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase postedFile)
    {
        byte[] bytes;
        using (BinaryReader br = new BinaryReader(postedFile.InputStream))
        {
            bytes = br.ReadBytes(postedFile.ContentLength);
        }
        string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Name", Path.GetFileName(postedFile.FileName));
                cmd.Parameters.AddWithValue("@ContentType", postedFile.ContentType);
                cmd.Parameters.AddWithValue("@Data", bytes);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
 
        return View(GetFiles());
    }
 
    [HttpGet]
    public FileResult DownloadFile(int? fileId)
    {
        byte[] bytes;
        string fileName, contentType;
        string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT Name, Data, ContentType FROM tblFiles WHERE Id=@Id";
                cmd.Parameters.AddWithValue("@Id", fileId);
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    sdr.Read();
                    bytes = (byte[])sdr["Data"];
                    contentType = sdr["ContentType"].ToString();
                    fileName = sdr["Name"].ToString();
                }
                con.Close();
            }
        }
 
        return File(bytes, contentType, fileName);
    }
 
    private static List<FileModel> GetFiles()
    {
        List<FileModel> files = new List<FileModel>();
        string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Id, Name FROM tblFiles"))
            {
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        files.Add(new FileModel
                        {
                            Id = Convert.ToInt32(sdr["Id"]),
                            Name = sdr["Name"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }
        return files;
    }
}
 
 
View
Inside the View, in the very first line the FileModel class is declared as IEnumerable which specifies that it will be available as a Collection.
The View consists of two Forms:-
Form for Uploading the File
This Form consists of an HTML FileUpload and a Submit Button. When the Button is clicked, the Index Action method for handling POST operation will be called.
 
Playing (Live Streaming) of Video Files
For playing (live streaming) of video files, a Flash based video player named FlowPlayer plugin is used. A loop is executed over the Model which generates an HTML Table. Each cell of HTML Table consists of an HTML Anchor element to whose HREF attribute is set to the DownloadFile Action method.
The FlowPlayer plugin is applied to each Anchor element using its CSS class which converts it into a Flash video player.
@model  IEnumerable<Videos_Database_MVC.Models.FileModel>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="postedFile"/>
        <input type="submit" id="btnUpload" value="Upload"/>
    }
    <hr/>
    <table cellpadding="0" cellspacing="0">
        @if (Model.Count() > 0)
        {
            foreach (var file in Model)
            {
                <tr>
                    <td>
                        <u>
                            @file.Name
                        </u>
                        <hr/>
                        <a class="player" style="height: 300px; width: 300px; display: block" href='@Url.Action("DownloadFile", "Home", new {fileId = file.Id})'>
                        </a>
                    </td>
                </tr>
            }
        }
    </table>
    <script src="~/FlowPlayer/flowplayer-3.2.12.min.js"></script>
    <script type="text/javascript">
    flowplayer("a.player", "/FlowPlayer/flowplayer-3.2.16.swf", {
        plugins: {
            pseudo: { url: "/FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf" }
        },
        clip: { provider: 'pseudo', autoPlay: false},
    });
    </script>
</body>
</html>
 
 
Screenshot
Video Player when not playing (streaming) videos
Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net MVC
 
Video Player when playing (streaming) videos
Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net MVC
 
 
Downloads