0

How can I show in 1 view all the vidoes in a directory? What is the best approach for this? Here is the simple code I am using for 1 video. Also is there a way to disable right click download?

Thanks!

<video controls="controls" width="465" height="315">
   <source src="~/Videos/Bunny2.mp4" type="video/mp4">
 </video>
SEBTulsa
  • 45
  • 6

1 Answers1

0

The Directory.EnumerateFiles method will provide a list of files in a directory. It returns just a list of full filepath strings. I'd use that in a class constructor, pulling info like Name and Type out of the string, and do something like this:

// Controller action
public ActionResult GetVideos()
{
    var videos = Directory.EnumerateFiles("Videos/");
    var videoList = new List<Video>();
    foreach (var video in videos)
    {
        videoList.Add(new Video(video));
    }

    return View(videoList);
}

// Razor view
@model IEnumerable<Video>

@foreach (var video in Model)
{
    <video controls="controls" width="465" height="315">
        <source src="@video.Source" type="@video.Type">
    </video>
}

To prevent download, disable the context menu. The MDN Element: contextmenu event has the following to disable this, though I would use a class you've applied to all the video elements.

noContext = document.getElementById('noContextMenu');

noContext.addEventListener('contextmenu', e => {
  e.preventDefault();
});
Nathan Miller
  • 785
  • 4
  • 11
  • This is exactly what I needed. Quick question - is there a way to add captions below or about each video easily? – SEBTulsa Dec 18 '19 at 17:28
  • Yes, but not that easily. There's some [Microsoft docs](http://msdn.microsoft.com/en-us/library/windows/desktop/ff384862(v=vs.85).aspx) on it, and [some SO questions](https://stackoverflow.com/questions/220097/read-write-extended-file-properties-c) – Nathan Miller Dec 18 '19 at 17:46