- 1 year ago
Asp.Net Core Web App Play and download Video from Disk
To play and download videos from disk in an ASP.NET Core web application, you can follow these steps:
Create a Controller: Create a controller in your ASP.NET Core application to handle the video playback and downloading.
csharppublic class VideoController : Controller
{
// Action for playing the video
public IActionResult PlayVideo(string videoFileName)
{
var videoPath = Path.Combine("path_to_videos_folder", videoFileName);
return PhysicalFile(videoPath, "video/mp4");
}
// Action for downloading the video
public IActionResult DownloadVideo(string videoFileName)
{
var videoPath = Path.Combine("path_to_videos_folder", videoFileName);
return PhysicalFile(videoPath, "application/octet-stream", enableRangeProcessing: true);
}
}
Replace
"path_to_videos_folder"
with the actual path to the folder where your videos are stored.Configure Routing: In your
Startup.cs
file, configure the routing to map the video-related URLs to the appropriate controller actions.csharppublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "video",
pattern: "Video/{action}/{videoFileName?}",
defaults: new { controller = "Video" });
});
}
Link to Video Playback and Download: In your HTML views, create links or embed a video player to play and download the videos.
html<!-- Link to play the video -->
<a href="/Video/PlayVideo/videoFileName.mp4">Play Video</a>
<!-- Link to download the video -->
<a href="/Video/DownloadVideo/videoFileName.mp4">Download Video</a>
<!-- Embed a video player -->
<video src="/Video/PlayVideo/videoFileName.mp4" controls></video>
Replace
"videoFileName.mp4"
with the actual filename of the video you want to play or download.
Make sure that the videos are accessible to the web application by placing them in a location that the application has read access to. Also, consider security measures such as authorization or protecting access to the videos if required.
By following these steps, you should be able to play and download videos from disk in your ASP.NET Core web application.