Click here to Skip to main content
15,891,033 members

Comments by K I R T I (Top 1 by date)

K I R T I 1-Aug-17 1:12am View    
namespace WebApplication1.Controllers
{
public class VideoController : Controller
{
// GET: Video
public ActionResult Index()
{
VideoContext entity = new VideoContext();
var data = entity.Videos.ToList();
return View(data);
}

// GET: Video/Create
public ActionResult Create()
{
return View();
}

// POST: Video/Create
[HttpPost]
public ActionResult Create(VideoViewModel model)
{
try
{
if (ModelState.IsValid)
{
var uploadFilesDir = Server.MapPath("~/Video");
var fileSavePath = Path.Combine(uploadFilesDir, model.file.FileName);

int length = model.file.ContentLength;
byte[] video = new byte[length];

model.file.InputStream.Read(video, 0, length);
model.file.SaveAs(fileSavePath);

Video.DataModel.Video obj = new Video.DataModel.Video();
obj.VideoName = model.VideoName;
obj.VideoPath = model.file.FileName;
obj.Image = video;

VideoContext entity = new VideoContext();
entity.Videos.Add(obj);
entity.SaveChanges();

return RedirectToAction("Index");
}

return View();
}
catch (Exception ex)
{
return View();
}
}

[HttpGet]
public ActionResult PlayVideo(int VideoId)
{
VideoContext entity = new VideoContext();
var data = entity.Videos.Where(x => x.VideoId.Equals(VideoId)).FirstOrDefault();
var base64 = Convert.ToBase64String(data.Image);
var imgSrc = String.Format("data:video/mp4;base64,{0}", base64);

data.VideoPath = imgSrc;

return PartialView(data);
}

public ActionResult CreateNew()
{
return View();
}
}
}