In this article I will explain with an example, how to upload (insert) file into SQL Server Database in ASP.Net MVC.
 
 
Database
This article makes use of table named tblFiles whose schema is defined as follows.
Upload (Insert) into SQL Server Database in ASP.Net MVC
 
Note: You can download the database table SQL by clicking the download link below.  
         Download SQL file
 
 
Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
 
 
Controller
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside the Action method, simply the View is returned.
 
Action method for handling POST operation
This Action method gets called, when a File is selected and the Upload Button is clicked.
Inside this Action method, the uploaded file is received in the HttpPostedFileBase as parameter.
Note: In case the HttpPostedFileBase parameter is appearing NULL, please refer my article, ASP.Net MVC: HttpPostedFileBase always returns NULL.
 
Then, the uploaded File is converted into an Array of Bytes using BinaryReader class and finally, inserted into the database table.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase postedFile)
    {
        byte[] bytes;
        string fileName = Path.GetFileName(postedFile.FileName);
        string contentType = postedFile.ContentType;
        using (BinaryReader br = new BinaryReader(postedFile.InputStream))
        {
            bytes = br.ReadBytes(postedFile.ContentLength);
        }
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Name", fileName);
                cmd.Parameters.AddWithValue("@ContentType", contentType);
                cmd.Parameters.AddWithValue("@Data", bytes);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        return View();
    }
}
 
 
View
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The HTML Form has been specified with enctype=“multipart/form-data” attribute as it is necessary for File Upload operation.
Inside the HTML Form there is an HTML FileUpload and Button control.
 
Form Submit
When the Upload Button is clicked the form is submitted to the Controllers Action Method.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="postedFile" />
        <input type="submit" id="btnUpload" value="Upload" />
    }
</body>
</html>
 
 
Screenshots
Upload (Insert) into SQL Server Database in ASP.Net MVC
 
The inserted files in Database table
Upload (Insert) into SQL Server Database in ASP.Net MVC
 
 
Downloads