如何在 1 个视图中显示目录中的所有视频?最好的方法是什么?这是我用于 1 个视频的简单代码。还有没有办法禁用右键单击下载?
谢谢!
<video controls="controls" width="465" height="315">
<source src="~/Videos/Bunny2.mp4" type="video/mp4">
</video>
该Directory.EnumerateFiles方法将提供目录中的文件列表。它只返回完整文件路径字符串的列表。我会在类构造函数中使用它,从字符串中提取诸如 Name 和 Type 之类的信息,然后执行以下操作:
// 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>
}
要防止下载,请禁用上下文菜单。该MDN元素:contextmenu事件已禁用此功能,但我会用你已经应用到所有的视频内容类以下。
noContext = document.getElementById('noContextMenu');
noContext.addEventListener('contextmenu', e => {
e.preventDefault();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句