使用 c# ASP.NET MVC 获取要播放的视频
Getting Video To play using c# ASP.NET MVC
我正在尝试让人们将视频保存在数据库中,然后其他用户可以在线观看这些视频。
我将文件保存在 wwwroot/videos/Username 中,同时将其他信息(视频名称、描述、url)保存在数据库中。已保存所有文件,但似乎无法播放视频。做了一些研究后发现,出于安全原因,firefox 和 chrome 无法播放您磁盘中的视频。将 IFormFile 视频文件正确保存到数据库并收工是否更好?或者有没有办法使这项工作?
上传并保存视频:
[HttpPost]
public async Task<IActionResult> UploadVideo(VideoViewModel video, IFormFile file)
{
if (file.Length > 0)
{
try
{
string userName = (User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Name).Value);
string pathString = Path.Combine(@"path", userName);
if (!Directory.Exists(pathString))
{
Directory.CreateDirectory(pathString);
}
int fCount = Directory.GetFiles(pathString, "*", SearchOption.TopDirectoryOnly).Length + 1;
var filePath = Path.Combine(pathString, file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
_videoLogic.SaveVideo(new Video(video.VideoId, video.Description, file.FileName, DateTime.Now, filePath, video.CategoryId));
}
}
catch (Exception ex)
{
ViewBag.Message = "ERROR: " + ex.Message.ToString();
}
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return RedirectToAction("UploadVideo");
}
正在尝试观看视频:
<h2>Overzicht Videos gebruiker</h2>
<!DOCTYPE html>
<html>
<head>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
@foreach(var item in Model)
{
<div class="col-sm-4 col-md-4 col-xs-12">
<div class="modal-title" style="width:250px;">@item.Name</div>
<hr/>
<div class="video-frame">
<video style="width:250px; height:150px;" controls>
<source src="@Url.Content(item.ContentUrl)" type="video/mp4" />
</video>
</div>
</div>
}
</div>
</body>
</html>
控制器中获取视频的代码:
[HttpGet]
public ActionResult GetVideosUser(UserViewModel user)
{
List<VideoViewModel> videoViewModels = new List<VideoViewModel>();
string userName = user.FirstName + " " + user.LastName;
string pathString = Path.Combine(@"path", userName);
List<Video> videos = _videoLogic.GetVideos();
foreach (Video video in videos)
{
VideoViewModel videoViewModel = new VideoViewModel(video);
videoViewModels.Add(videoViewModel);
}
return View("ViewerVideoList", videoViewModels);
}
此代码正确地将数据上传到我的数据库,并将正确的文件上传到我的视频文件夹。
它还正确获取了上述 ContentUrl。
我怎样才能让这个视频正确播放?
非常感谢!
代码图片:
![代码]: https://imgur.com/a/xdprrAw
URL 在 Chrome 中:/wwwroot/video/Jesse%20Oosterwijk/WhatsApp%20Video%202019-12-04%20at%2018.04.49.mp4
![Chrome代码]: https://imgur.com/a/kCM3p71
![FileIsThere]: https://imgur.com/a/WaXQtUd
CorrectExtensions
https://imgur.com/a/KTI2vSv
Did some research and saw that because of security reasons, firefox and chrome, are not able to play video's from your disk.
您可以尝试通过相对于 web root 的路径而不是物理路径来访问这些视频文件。
Name = "user1",
ContentUrl = "~/videos/user1/movie.mp4"
测试结果
注意:出现问题时,如果您在浏览器中查看控制台或 html 源,您可能会发现文件路径如下所示。
找到解决问题的方法。通过依赖项注入,我可以将 IHostingEnvironment 添加到我的视频控制器。这样我可以更轻松地将视频保存到我的 webroot。
添加托管环境:
public class VideoController : Controller
{
private readonly VideoLogic _videoLogic;
private readonly CategoryLogic _categoryLogic;
private readonly ReportLogic _reportLogic;
private readonly CommentLogic _commentLogic;
private readonly UserLogic _userLogic;
private readonly IHostingEnvironment _environment;
public VideoController(VideoLogic videoLogic, CategoryLogic categoryLogic, ReportLogic reportLogic, CommentLogic commentLogic, UserLogic userLogic, IHostingEnvironment environment)
{
_videoLogic = videoLogic;
_categoryLogic = categoryLogic;
_reportLogic = reportLogic;
_commentLogic = commentLogic;
_userLogic = userLogic;
_environment = environment;
}
在保存视频的代码中:
[HttpPost]
public async Task<IActionResult> UploadVideo(VideoViewModel video, IFormFile file)
{
if (file.Length > 0)
{
try
{
int userId = int.Parse(User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Sid)?.Value);
string userName = (User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Name).Value);
string folder = Path.Combine(_environment.WebRootPath, "video");
string url = @"\video\" + userName + @"\" + file.FileName;
string pathString = Path.Combine(folder, userName);
if (!Directory.Exists(pathString))
{
Directory.CreateDirectory(pathString);
}
var filePath = Path.Combine(pathString, file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
_videoLogic.SaveVideo(new Video(userId, video.VideoId, video.Description, file.FileName, DateTime.Now, url, video.CategoryId));
}
}
catch (Exception ex)
{
ViewBag.Message = "ERROR: " + ex.Message.ToString();
}
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return RedirectToAction("UploadVideo");
}
之后你就可以从数据库中获取视频并从那里获取 url!
感谢大家的帮助
我正在尝试让人们将视频保存在数据库中,然后其他用户可以在线观看这些视频。 我将文件保存在 wwwroot/videos/Username 中,同时将其他信息(视频名称、描述、url)保存在数据库中。已保存所有文件,但似乎无法播放视频。做了一些研究后发现,出于安全原因,firefox 和 chrome 无法播放您磁盘中的视频。将 IFormFile 视频文件正确保存到数据库并收工是否更好?或者有没有办法使这项工作?
上传并保存视频:
[HttpPost]
public async Task<IActionResult> UploadVideo(VideoViewModel video, IFormFile file)
{
if (file.Length > 0)
{
try
{
string userName = (User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Name).Value);
string pathString = Path.Combine(@"path", userName);
if (!Directory.Exists(pathString))
{
Directory.CreateDirectory(pathString);
}
int fCount = Directory.GetFiles(pathString, "*", SearchOption.TopDirectoryOnly).Length + 1;
var filePath = Path.Combine(pathString, file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
_videoLogic.SaveVideo(new Video(video.VideoId, video.Description, file.FileName, DateTime.Now, filePath, video.CategoryId));
}
}
catch (Exception ex)
{
ViewBag.Message = "ERROR: " + ex.Message.ToString();
}
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return RedirectToAction("UploadVideo");
}
正在尝试观看视频:
<h2>Overzicht Videos gebruiker</h2>
<!DOCTYPE html>
<html>
<head>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
@foreach(var item in Model)
{
<div class="col-sm-4 col-md-4 col-xs-12">
<div class="modal-title" style="width:250px;">@item.Name</div>
<hr/>
<div class="video-frame">
<video style="width:250px; height:150px;" controls>
<source src="@Url.Content(item.ContentUrl)" type="video/mp4" />
</video>
</div>
</div>
}
</div>
</body>
</html>
控制器中获取视频的代码:
[HttpGet]
public ActionResult GetVideosUser(UserViewModel user)
{
List<VideoViewModel> videoViewModels = new List<VideoViewModel>();
string userName = user.FirstName + " " + user.LastName;
string pathString = Path.Combine(@"path", userName);
List<Video> videos = _videoLogic.GetVideos();
foreach (Video video in videos)
{
VideoViewModel videoViewModel = new VideoViewModel(video);
videoViewModels.Add(videoViewModel);
}
return View("ViewerVideoList", videoViewModels);
}
此代码正确地将数据上传到我的数据库,并将正确的文件上传到我的视频文件夹。 它还正确获取了上述 ContentUrl。 我怎样才能让这个视频正确播放? 非常感谢!
代码图片: ![代码]: https://imgur.com/a/xdprrAw
URL 在 Chrome 中:/wwwroot/video/Jesse%20Oosterwijk/WhatsApp%20Video%202019-12-04%20at%2018.04.49.mp4
![Chrome代码]: https://imgur.com/a/kCM3p71
![FileIsThere]: https://imgur.com/a/WaXQtUd
CorrectExtensions https://imgur.com/a/KTI2vSv
Did some research and saw that because of security reasons, firefox and chrome, are not able to play video's from your disk.
您可以尝试通过相对于 web root 的路径而不是物理路径来访问这些视频文件。
Name = "user1",
ContentUrl = "~/videos/user1/movie.mp4"
测试结果
注意:出现问题时,如果您在浏览器中查看控制台或 html 源,您可能会发现文件路径如下所示。
找到解决问题的方法。通过依赖项注入,我可以将 IHostingEnvironment 添加到我的视频控制器。这样我可以更轻松地将视频保存到我的 webroot。
添加托管环境:
public class VideoController : Controller
{
private readonly VideoLogic _videoLogic;
private readonly CategoryLogic _categoryLogic;
private readonly ReportLogic _reportLogic;
private readonly CommentLogic _commentLogic;
private readonly UserLogic _userLogic;
private readonly IHostingEnvironment _environment;
public VideoController(VideoLogic videoLogic, CategoryLogic categoryLogic, ReportLogic reportLogic, CommentLogic commentLogic, UserLogic userLogic, IHostingEnvironment environment)
{
_videoLogic = videoLogic;
_categoryLogic = categoryLogic;
_reportLogic = reportLogic;
_commentLogic = commentLogic;
_userLogic = userLogic;
_environment = environment;
}
在保存视频的代码中:
[HttpPost]
public async Task<IActionResult> UploadVideo(VideoViewModel video, IFormFile file)
{
if (file.Length > 0)
{
try
{
int userId = int.Parse(User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Sid)?.Value);
string userName = (User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Name).Value);
string folder = Path.Combine(_environment.WebRootPath, "video");
string url = @"\video\" + userName + @"\" + file.FileName;
string pathString = Path.Combine(folder, userName);
if (!Directory.Exists(pathString))
{
Directory.CreateDirectory(pathString);
}
var filePath = Path.Combine(pathString, file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
_videoLogic.SaveVideo(new Video(userId, video.VideoId, video.Description, file.FileName, DateTime.Now, url, video.CategoryId));
}
}
catch (Exception ex)
{
ViewBag.Message = "ERROR: " + ex.Message.ToString();
}
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return RedirectToAction("UploadVideo");
}
之后你就可以从数据库中获取视频并从那里获取 url! 感谢大家的帮助