In MVC4, you may encounter the situation that your website privides a video playing screen so that video stored in MS SQL filestream or FileTable database have to be played after click the video file.

What problem does the sample solve?

The idea is that user access your website, not your database. You shall have a list of video files in your webpage.  And a URL to lcick the video file to play the video. So your MVC4 project query FileStream / Fieltable database, get the video file and convert into binery stream, and then feed the video playing screen to play the video.

Are there special requirements or instructions for building the sample?

Your website will get the datasteam from the database and then played in the video screen. There is no special requirements. The instruction for builing the project involved a regular MVC4 project together with your MSSQL with filestream or FileTable database. You should have the right query method to get the files and list the files with property you want.

How does this sample solve the problem?

You can include code snippets, images, videos.   

 

C#
Edit|Remove
1. In your View: 
a).  Button for playing the video: 
<td class="playVideoTD">                         
   @{Html.BeginForm("Index""Video",  new { id = item.StreamID }, FormMethod.Post);} 
                                <input type="submit" value="" class="playVideoIcon"/> 
                            @{Html.EndForm();} 
 </td> 
 
b). Video playing windows: 
<div class="playVideoDiv">                   
    <video id="videoPlay"  width="480" height="400"  controls autoplay> 
<source src= "@Url.Action("GetVideo", "Video", new { id = ViewBag.ID } )"  />  
    </video> 
</div>  
 
2. In controller called VideoController 
 
   [HttpPost] 
        public ActionResult Index(Guid id) 
        { 
            FileTableInfo fi = new FileTableInfo(); 
            ViewBag.ID = id; 
            List<FileTableInfo> Lfi = fi.GetListofFileInfo(); 
            return PartialView(Lfi); 
        } 
 
        public ActionResult GetVideo(Guid id) 
        { 
            FileTableInfo fi = new FileTableInfo(); 
           // DataTable file = fi.GetAFile(id);    //using old way of access database 
            DataTable file = fi.GetAFileFromFileTable(id); 
            DataRow row = file.Rows[0]; 
 
            string name = (string)row["video_name"]; 
            string contentType = (string)row["content_Type"]; 
            Byte[] video = (Byte[])row["data"]; 
            string mimeType; 
            switch (contentType.ToUpper()) 
            { 
                case "MOV": 
                    mimeType = "video/quicktime"; 
                    break; 
                case "MP4": 
                    mimeType = "video/mp4"; 
                    break; 
                case "FLV": 
                    mimeType = "video/x-flv"; 
                    break; 
                case "AVI": 
                    mimeType = "video/x-msvideo"; 
                    break; 
                case "WMV": 
                    mimeType = "video/x-ms-wmv"; 
                    break; 
                case "MJPG": 
                    mimeType = "video/x-motion-jpeg"; 
                    break; 
                case "TS": 
                    mimeType = "video/MP2T"; 
                    break; 
                default: 
                    mimeType = "video/mp4";    
                    break; 
            } 
            return File(video, mimeType);  
        } 
 
3. In your model, you may add a class of FileTableInfo: 
 
 public class FileTableInfo 
    { 
        public Guid StreamID { getset; } 
        public string FileName { getset; } 
        public string FileType { getset; } 
        public Int64 FileSize { getset; } 
        public int CarId { getset; } 
        public int SubInstId { getset; } 
        public string VideoStart { getset; } 
        public string VideoEnd { getset; } 
        public string Note { getset; } 
 
        public FileTableInfo() 
        { 
        } 
        /// <summary> 
        /// Construtor 
        /// </summary> 
        /// <param name="_trainId"></param> 
        /// <param name="_health"></param> 
        /// <param name="_hb_count"></param> 
        public FileTableInfo(Guid streamId,  string fileName, string fileType, Int64 fileSize, int carId, int subInstId,  
            string videoStart, string videoEnd, string note) 
        { 
            this.StreamID = streamId; 
            this.FileName = fileName; 
            this.FileType = fileType; 
            this.FileSize = fileSize; 
            this.CarId = carId; 
            this.SubInstId = subInstId; 
            this.VideoStart = videoStart; 
            this.VideoEnd = videoEnd; 
            this.VideoEnd = videoEnd; 
            this.Note = note; 
        } 
 
 
        /// <summary> 
        /// Constructor 
        /// </summary> 
        /// <param name="train_id"></param> 
        public List<FileTableInfo> GetListofFileInfo()   //StoredVideoFiles 
        { 
            List<FileTableInfo> listFileInfo = new List<FileTableInfo>(); 
            AbstractFactory df = new SQLFactory("DBName"); 
            DatabaseRequest req = new DatabaseRequest(); 
            req.CommandType = System.Data.CommandType.Text; 
            req.Command = "SELECT video_id, video_name, content_type, size, car_id, subsystem_instance_id, video_start, video_end, note FROM video "; 
           // req.Parameters.Add(new DatabaseRequest.Parameter("@dirName", dirName, SqlDbType.NVarChar)); 
 
            IDataReader reader = df.ExecuteDataReader(req); 
 
            while (reader.Read()) 
            { 
                Guid id = reader.GetGuid(0); 
                string name = reader.GetString(1); 
                string startDate = reader.GetDateTime(6).ToString("yyyy-MM-dd hh:mm", CultureInfo.CreateSpecificCulture("en-US")); 
                string endDate   = reader.GetDateTime(7).ToString("yyyy-MM-dd hh:mm", CultureInfo.CreateSpecificCulture("en-US")); 
                FileTableInfo fInfo = new FileTableInfo(id, name, reader.GetString(2), reader.GetInt64(3), 
                    reader.GetInt32(4), reader.GetInt32(5), startDate, endDate, reader.GetString(8)); 
                listFileInfo.Add(fInfo); 
            } 
            reader.Close(); 
            return listFileInfo; 
        } 
 
        // Get a file from the database by ID 
        /// <summary> 
        ///  
        /// </summary> 
        /// <param name="id"></param> 
        /// <returns></returns> 
        public DataTable GetAFileFromFileTable(Guid id) 
        { 
            DataTable file = new DataTable(); 
 
            AbstractFactory df = new SQLFactory("DBName"); 
            DatabaseRequest req = new DatabaseRequest(); 
            req.CommandType = System.Data.CommandType.Text; 
            req.Command = "SELECT video_name, content_type, data from video where  video_id=@ID"; 
            req.Parameters.Add(new DatabaseRequest.Parameter("@Id", id, SqlDbType.UniqueIdentifier)); 
            IDataReader reader = df.ExecuteDataReader(req); 
            while (!reader.IsClosed) 
            { 
                file.Load(reader); 
            } 
            reader.Close(); 
            return file; 
        } 

More Information

For more information on X, see ...?