Introduction
This article demonstrates how to upload an audio file up to 50MB and play dynamically using ASP.NET MVC 5. I will upload the audio file in my project folder (AudioFileUpload) and its path in SQL Server database table.
I will retrieve (display) all the uploaded audio files and their related data in HTML table with sorting, searching, and paging functionality using Bootstrap 4 and jQuery data table plugin. We will understand this step by step.
Step 1
Create a database in SQL Server of your choice.
- CREATE TABLE [dbo].[AudioFiles](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NULL,
- [FileSize] [int] NULL,
- [FilePath] [nvarchar](100) NULL,
- CONSTRAINT [PK_AudioFiles] PRIMARY KEY 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 procedure [dbo].[spAddNewAudioFile]
- (
- @Name nvarchar(50),
- @FileSize int,
- @FilePath nvarchar(100)
- )
- as
- begin
- insert into AudioFiles (Name,FileSize,FilePath)
- values (@Name,@FileSize,@FilePath)
- end
- CREATE procedure [dbo].[spGetAllAudioFile]
- as
- begin
- select ID,Name,FileSize,FilePath from AudioFiles
- end

Step 2
Create an empty ASP.NET Web Application project in Visual Studio 2015. As shown in the below screenshot, open Visual Studio ( VS 2015 in my case). Click on the new project, choose web template >> choose ASP.NET Web Application, and give it a meaningful name. Then, click OK.

Choose empty from ASP.NET Templates, select checkbox MVC, and click on OK.

Step 3
Double click and open webconfig file to add the database connection.
- <connectionStrings>
- <add name="DBCS" connectionString="data source=FARHAN\SQLEXPRESS; database=MvcDemo; integrated security=true;" />
- </connectionStrings>
Add this below code in webconfig file to allow 50MB file to upload.
- <httpRuntime executionTimeout="3600" maxRequestLength="51200" enable="true" />
- <system.webServer>
- <security>
- <requestFiltering>
- <requestLimits maxAllowedContentLength="104857600" />
- </requestFiltering>
- </security>
- </system.webServer>
Step 4
Right-click on the project, add New Folder, name it AudioFileUpload to upload all audio files in that folder.
Step 5
Right click on Models folder, select Add >> Class.
Screenshot for creating model class-1

A window will appear. Choose Class, give it a name as AudioFile, then click on Add.
Screenshot for creating model class-2

Write class field and property as we have in the database table.
- using System;
- namespace MvcUploadAudioFile_Demo.Model
- {
- public class AudioFile
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public Nullable<int> FileSize { get; set; }
- public string FilePath { get; set; }
- }
- }
Step 6
Right-click on Controllers folder, select Add >> Controller. Name it as HomeController.
Screenshot for creating controller 1

After clicking on Controller, a window will appear. Choose MVC 5 Controller-Empty and click on Add.
Screenshot for creating controller 2

After that, another window will appear with DefaultController highlighted by default. Change the default to HomeController. Remember, don’t change suffix name of the Controller here. Click on Add and the Home controller will be added to the Controllers folder.
Screenshot for creating controller 3

Add the following namespace in Controller class
- using MvcUploadAudioFile_Demo.Model;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.IO;
Create UploadAudio action method with [HttpGet] in controller. Write the following code to retrieve the data from database table.
- [HttpGet]
- public ActionResult UploadAudio()
- {
- List<AudioFile> audiolist = new List<AudioFile>();
- string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd = new SqlCommand("spGetAllAudioFile", con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- AudioFile audio = new AudioFile();
- audio.ID = Convert.ToInt32(rdr["ID"]);
- audio.Name = rdr["Name"].ToString();
- audio.FileSize = Convert.ToInt32(rdr["FileSize"]);
- audio.FilePath = rdr["FilePath"].ToString();
- audiolist.Add(audio);
- }
- }
- return View(audiolist);
- }
Create UploadAudio action method with [HttpPost] in controller. Write the following code to insert data into database and upload file in AudioFileUpload folder of project.
- [HttpPost]
- public ActionResult UploadAudio(HttpPostedFileBase fileupload)
- {
- if (fileupload != null)
- {
- string fileName = Path.GetFileName(fileupload.FileName);
- int fileSize = fileupload.ContentLength;
- int Size = fileSize/1000000;
- fileupload.SaveAs(Server.MapPath("~/AudioFileUpload/" + fileName));
- string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd = new SqlCommand("spAddNewAudioFile", con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- cmd.Parameters.AddWithValue("@Name", fileName);
- cmd.Parameters.AddWithValue("@FileSize", Size);
- cmd.Parameters.AddWithValue("FilePath", "~/AudioFileUpload/" + fileName);
- cmd.ExecuteNonQuery();
- }
- }
- return RedirectToAction("UploadAudio");
- }
Complete code for HomeController to insert and retrieve file.





Adnan ZafarPosted Jul 20, 2019, 6:18 AM
Im getting an error in config file HTTP Error 500.19 - Internal Server Error when i pasted <httpRuntime executionTimeout="3600" maxRequestLength="51200" enable="true" /> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="104857600" /> </requestFiltering> </security> </system.webServer>
Vishnu GhPosted Mar 19, 2019, 11:55 PM
Please this project send me Sir In Your Source Code in Zip file Sir ... I Want Immediately
Indra GrahaPosted Aug 20, 2018, 4:17 AM
Can u share the sourcecode bro?