Asp.net Core Dynamically Create a File Upload
Introduction
This article demonstrates how to upload a video file of up to 100MB and play dynamically using ASP.Internet MVC 5. I will upload the video file to my project folder proper noun (VideoFileUpload) and add its path in SQL Server database tabular array. I will display all uploaded video files and their names.
Pace 1
Create a database in the SQL Server of your choice.
- CREATE TABLE [dbo].[VideoFiles](
- [ID] [int ] IDENTITY(ane,1) NOT NULL ,
- [Name ] [nvarchar](50) Cypher ,
- [FileSize] [int ] Zilch ,
- [FilePath] [nvarchar](100)NULL ,
- CONSTRAINT [PK_VideoFiles] Chief Primal CLUSTERED
- (
- [ID]ASC
- )WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ Primary ]
- )ON [ PRIMARY ]
- GO
- CREATE process [dbo].[spAddNewVideoFile]
- (
- @Name nvarchar(50),
- @FileSizeint ,
- @FilePath nvarchar(100)
- )
- as
- begin
- insert into VideoFiles ( Name ,FileSize,FilePath)
- values (@ Name ,@FileSize,@FilePath)
- end
- CREATE procedure [dbo].[spGetAllVideoFile]
- as
- begin
- select ID, Proper noun ,FileSize,FilePath from VideoFiles
- end
Step ii
Open Visual Studio 2015. Create an empty ASP.Net Web Application projection in it. As shown in below screenshot, click on new project >> choose web template >> cull ASP.Net Web Awarding and give it a meaningful name to click ok.
Screenshot one
Choose empty from ASP.Internet Templates, select checkbox MVC, and click on OK, every bit shown in the image below.
Screenshot two
Step 3
Double click and open up web config file to add the database connexion.
- <connectionStrings>
- <add proper noun="DBCS" connectionString= "data source=FARHAN\SQLEXPRESS; database=MvcDemo; integrated security=true;" />
- </connectionStrings>
Add the below code in webconfig file to allow 100MB file to upload.
- <httpRuntime executionTimeout= "3600" maxRequestLength= "104857600" enable= "truthful" />
- <organisation.webServer>
- <security>
- <requestFiltering>
- <requestLimits maxAllowedContentLength="104857600" />
- </requestFiltering>
- </security>
- </system.webServer>
Stride 4
Right-click on projection Add select New Binder, proper name it VideoFileUpload to upload all the audio files in that folder.
Step 5Right-click on Models binder, select Add, so select Class.
Screenshot for creating model class one
A window will announced. Cull Form, give information technology the proper noun VideoFiles, then click on Add.
Screenshot for creating model class 2
Write grade field and belongings as we have in the database table.
- using Organization;
- using System.Collections.Generic;
- using Organization.Linq;
- using System.Spider web;
- namespace MvcUploadVideoFile_Demo.Models
- {
- public class VideoFiles
- {
- public int ID { become ; ready ; }
- public string Proper name { become ; set ; }
- public Nullable< int > FileSize { become ; prepare ; }
- public string FilePath { become ; set ; }
- }
- }
Step 6
Right-click on Controllers folder, select Add together, and so select Controller. HomeController.
Screenshot for creating controller one
After clicking on controller a window will announced; choose MVC v Controller-Empty and click on Add.
Screenshot for creating controller two
After that, some other window will appear with DefaultController. Call back, don't modify the suffix name of controller hither. Click on Add and a Home controller will be added in Controllers folder.
Screenshot for creating controller iii
Add the following namespace
- using MvcUploadVideoFile_Demo.Models;
- using Organisation.Configuration;
- using System.Data;
- using System.Information.SqlClient;
- using System.IO;
Create UploadVideo activeness method with [HttpGet] in controller. Write the following code to recall the data from the database table.
- [HttpGet]
- public ActionResult UploadVideo()
- {
- List<VideoFiles> videolist =new List<VideoFiles>();
- string CS = ConfigurationManager.ConnectionStrings["DBCS" ].ConnectionString;
- using (SqlConnection con =new SqlConnection(CS))
- {
- SqlCommand cmd =new SqlCommand( "spGetAllVideoFile" , con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- VideoFiles video =new VideoFiles();
- video.ID = Catechumen.ToInt32(rdr["ID" ]);
- video.Proper name = rdr["Name" ].ToString();
- video.FileSize = Convert.ToInt32(rdr["FileSize" ]);
- video.FilePath = rdr["FilePath" ].ToString();
- videolist.Add together(video);
- }
- }
- return View(videolist);
- }
Create UploadVideo action method with [HttpPost] in controller. Write following code to insert data into database and upload file in VideoFileUpload folder of projection.
- [HttpPost]
- public ActionResult UploadVideo(HttpPostedFileBase fileupload)
- {
- if (fileupload != nix )
- {
- string fileName= Path.GetFileName(fileupload.FileName);
- int fileSize = fileupload.ContentLength;
- int Size = fileSize / 1000;
- fileupload.SaveAs(Server.MapPath("~/VideoFileUpload/" + fileName));
- string CS = ConfigurationManager.ConnectionStrings["DBCS" ].ConnectionString;
- using (SqlConnection con =new SqlConnection(CS))
- {
- SqlCommand cmd =new SqlCommand( "spAddNewVideoFile" , con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open up();
- cmd.Parameters.AddWithValue("@Proper name" ,fileName);
- cmd.Parameters.AddWithValue("@FileSize" , Size);
- cmd.Parameters.AddWithValue("FilePath" , "~/VideoFileUpload/" + fileName);
- cmd.ExecuteNonQuery();
- }
- }
- return RedirectToAction( "UploadVideo" );
- }
Complete HomeController lawmaking
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using Arrangement.Data.SqlClient;
- using System.IO;
- using System.Web;
- using Arrangement.Web.Mvc;
- using MvcUploadVideoFile_Demo.Models;
- namespace MvcUploadVideoFile_Demo.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- [HttpGet]
- public ActionResult UploadVideo()
- {
- List<VideoFiles> videolist =new List<VideoFiles>();
- string CS = ConfigurationManager.ConnectionStrings[ "DBCS" ].ConnectionString;
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd =new SqlCommand( "spGetAllVideoFile" , con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- VideoFiles video =new VideoFiles();
- video.ID = Convert.ToInt32(rdr["ID" ]);
- video.Proper name = rdr["Name" ].ToString();
- video.FileSize = Convert.ToInt32(rdr["FileSize" ]);
- video.FilePath = rdr["FilePath" ].ToString();
- videolist.Add(video);
- }
- }
- return View(videolist);
- }
- [HttpPost]
- public ActionResult UploadVideo(HttpPostedFileBase fileupload)
- {
- if (fileupload != null )
- {
- cord fileName= Path.GetFileName(fileupload.FileName);
- int fileSize = fileupload.ContentLength;
- int Size = fileSize / 1000;
- fileupload.SaveAs(Server.MapPath("~/VideoFileUpload/" + fileName));
- string CS = ConfigurationManager.ConnectionStrings[ "DBCS" ].ConnectionString;
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd =new SqlCommand( "spAddNewVideoFile" , con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open up();
- cmd.Parameters.AddWithValue("@Name" ,fileName);
- cmd.Parameters.AddWithValue("@FileSize" , Size);
- cmd.Parameters.AddWithValue("FilePath" , "~/VideoFileUpload/" + fileName);
- cmd.ExecuteNonQuery();
- }
- }
- return RedirectToAction( "UploadVideo" );
- }
- }
- }
Pace vii
Right click on UploadVideo action method and Add together view (uncheck use a layout folio). Click on Add together.
Add the post-obit jquery script and bootstrap file in caput section of view page. Download or add package from NuGet Bundle Manager Click on Tools => NuGet Package Manager => Manage NuGet Bundle. Select Browse type bootstrap in search bar, and select and install like jquery. All downloaded files will be in content and scripts.
Add the following styles and scripts in the head section.
- <link rel= "stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-crawly.min.css" >
- <link href="~/Content/bootstrap.min.css" rel= "stylesheet" />
- <script src="~/scripts/jquery-3.3.1.min.js" ></script>
- <script src="~/scripts/bootstrap.min.js" ></script>
Write the following style for video frame and championship of video
- <way blazon= "text/css" >
- .video-frame {
- width : 100% ;
- height : 195px ;
- border : 4px solid #dc3545 ;
- box-shadow:1px 2px 3px #dc3545 ;
- border-radius:3px ;
- }
- .title {
- font-weight : 500 ;
- font-size : 14px ;
- text-marshal : eye ;
- margin-bottom : 10px ;
- margin-height : 10px ;
- background-color : #dc3545 ;
- color : white ;
- box-shadow:1px 2px 4px #dc3545 ;
- }
- </style>
Complete view lawmaking
- @{
- Layout =null ;
- }
- <!DOCTYPE html>
- <html>
- <caput>
- <meta name="viewport" content= "width=device-width" />
- <title>Upload Video</championship>
- <link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/iv.7.0/css/font-crawly.min.css" >
- <link href="~/Content/bootstrap.min.css" rel= "stylesheet" />
- <script src="~/scripts/jquery-3.three.ane.min.js" ></script>
- <script src="~/scripts/bootstrap.min.js" ></script>
- <style type="text/css" >
- .video-frame {
- width: 100%;
- pinnacle: 195px;
- border: 4px solid #dc3545;
- box-shadow: 1px 2px 3px #dc3545;
- border-radius: 3px;
- }
- .title {
- font-weight: 500;
- font-size: 14px;
- text-align: center;
- margin-bottom: 10px;
- margin-top: 10px;
- background-color: #dc3545;
- colour: white;
- box-shadow: 1px 2px 4px #dc3545;
- }
- </style>
- </head>
- <torso>
- <divgrade = "container py-4" >
- <h3class = "text-center text-majuscule" >How to Dynamically Upload and Play Video File Using ASP.NET MVC5</h3>
- <divcourse = "card" >
- <divclass = "card-header bg-danger text-white" >
- <h6class = "text-capital letter" >video List</h6>
- </div>
- <divform = "card-body" >
- <divclass = "row" >
- <button fashion="margin-left: 27px; margin-bottom:10px;" blazon= "push" class = "btn btn-danger rounded-0" information-toggle= "modal" information-target= "#UploadVideo" >
- <iclass = "fa fa-plus-circle" ></i> Add New
- </button>
- <divcourse = "modal fade" id= "UploadVideo" >
- <divclass = "modal-dialog" >
- <divclass = "modal-content" >
- <divcourse = "modal-header" >
- <h4class = "modal-title" >Upload New video File</h4>
- <button type="push" class = "close" information-dismiss= "modal" >×</push>
- </div>
- <divclass = "modal-body" >
- @using (Html.BeginForm("UploadVideo" , "Habitation" , FormMethod.Mail, new { enctype = "multipart/grade-information" }))
- {
- <divform = "class-group" >
- <label>Cull File:</label>
- <divform = "input-group" >
- <divclass = "custom-file" >
- <input blazon="file" id= "fileupload" name= "fileupload" course = "custom-file-input" />
- <labelclass = "custom-file-label" ></label>
- </div>
- <divgrade = "input-group-append" >
- <input blazon="submit" id= "btnUpload" class = "btn btn-secondary" value= "Upload" />
- </div>
- </div>
- </div>
- }
- </div>
- <divclass = "modal-footer" >
- <button type="button" class = "btn btn-danger" data-dismiss= "modal" >Close</button>
- </div>
- </div>
- </div>
- </div>
- </div>
- <divclass = "row" >
- @foreach (var detail in Model)
- {
- <divclass = "col-sm-4 col-doctor-four col-xs-12" >
- <divclass = "championship" >@item.Name</div>
- <divclass = "video-frame" >
- <video fashion="width:100%; height:auto;" controls>
- <source src="@Url.Content(@item.FilePath)" type= "video/mp4" />
- </video>
- </div>
- </div>
- }
- </div>
- </div>
- </div>
- </div>
- </torso>
- </html>
Step 8
Run Projection ctr+F5
Screenshot one
Screenshot two
Screenshot 3
Conclusion
In this article, I have explained how to upload and play a video file of up to 100MB in size using ASP.NET MVC5. I accept used bootstrap 4. I have explained a step by pace procedure with some screenshots.
Source: https://www.c-sharpcorner.com/article/how-to-dynamically-upload-and-play-video-file-using-asp-net-mvc-5/
0 Response to "Asp.net Core Dynamically Create a File Upload"
Post a Comment