Collecting image data or any other file format data is equally important as collecting textual data. Although collecting textual data is more simple than collecting an image or any other file format data, the choice for storing the image or any other file format data is the most difficult part, but, it entirely depends on one's business & system requirement.
Today, I shall be demonstrating uploading of the image file into the database on ASP.NET MVC5 platform. This article is not specific to image files only, you can use the provided solution with any type of file format as well.
Before moving to the coding part, let us observe some of the advantages and disadvantages of uploading an image or any other file format data into the database.
Advantages (Pros)
- Sensitive images or any other file format data is fully secure as it is only accessible via system/software. Files are not accessible via links.
- Storing of the uploaded file is guaranteed.
- Images/Files store on database do not require extra backups.
- Transnational integrity is guaranteed as you won't be locked into typical reader/writer problem and deleting the entry in the database means the file is actually deleted, you do not need extra precautions to delete the file i.e. ensuring that file is deleted from both database and file system.
- Image replication is easy (if needed).
- In load-balanced web servers or distributed environments, dealing with synchronizing images across multiple file systems is difficult especially in a web application where a new server may be added any time.
Disadvantages (Cons)
- Database storage becomes expensive for many and large file storage.
- There will be a performance penalty as latency to retrieve image/file is slower and database lookup is slower than filesystem lookup.
- Additional code is needed to extract and stream files.
- You cannot directly edit the files, especially images files, as you can not directly utilize image edit features such as file resize and file cropping.
- There will be more load on the database.
- Web server bandwidth will increase which adds additional costs.
Prerequisites
Following are some prerequisites before you proceed any further in this tutorial:
- Knowledge of ASP.NET MVC5.
- Knowledge of HTML.
- Knowledge of Bootstrap.
- Knowledge of C# Programming.
You can download the complete source code for this tutorial or you can follow the step by step discussion below. The sample code is being developed in Microsoft Visual Studio 2015 Enterprise.
Let's begin now.
Step 1
First, create your SQL server database and name it "db_img". Then execute following script into your SQL server database i.e.
- USE [db_img]
- GO
- /****** Object: StoredProcedure [dbo].[sp_insert_file] Script Date: 11/18/2018 12:27:55 AM ******/
- DROP PROCEDURE [dbo].[sp_insert_file]
- GO
- /****** Object: StoredProcedure [dbo].[sp_get_file_details] Script Date: 11/18/2018 12:27:55 AM ******/
- DROP PROCEDURE [dbo].[sp_get_file_details]
- GO
- /****** Object: StoredProcedure [dbo].[sp_get_all_files] Script Date: 11/18/2018 12:27:55 AM ******/
- DROP PROCEDURE [dbo].[sp_get_all_files]
- GO
- /****** Object: Table [dbo].[tbl_file] Script Date: 11/18/2018 12:27:55 AM ******/
- DROP TABLE [dbo].[tbl_file]
- GO
- /****** Object: Table [dbo].[tbl_file] Script Date: 11/18/2018 12:27:55 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[tbl_file](
- [file_id] [int] IDENTITY(1,1) NOT NULL,
- [file_name] [nvarchar](max) NOT NULL,
- [file_ext] [nvarchar](max) NOT NULL,
- [file_base6] [nvarchar](max) NOT NULL,
- CONSTRAINT [PK_tbl_file] PRIMARY KEY CLUSTERED
- (
- [file_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] TEXTIMAGE_ON [PRIMARY]
-
- GO
- /****** Object: StoredProcedure [dbo].[sp_get_all_files] Script Date: 11/18/2018 12:27:55 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
-
-
-
-
-
- CREATE PROCEDURE [dbo].[sp_get_all_files]
-
- AS
- BEGIN
- /****** Script for SelectTopNRows command from SSMS ******/
- SELECT [file_id]
- ,[file_name]
- ,[file_ext]
- FROM [db_img].[dbo].[tbl_file]
- END
-
- GO
- /****** Object: StoredProcedure [dbo].[sp_get_file_details] Script Date: 11/18/2018 12:27:55 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
-
-
-
-
-
- CREATE PROCEDURE [dbo].[sp_get_file_details]
- @file_id INT
- AS
- BEGIN
- /****** Script for SelectTopNRows command from SSMS ******/
- SELECT [file_id]
- ,[file_name]
- ,[file_ext]
- ,[file_base6]
- FROM [db_img].[dbo].[tbl_file]
- WHERE [tbl_file].[file_id] = @file_id
- END
-
- GO
- /****** Object: StoredProcedure [dbo].[sp_insert_file] Script Date: 11/18/2018 12:27:55 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
-
-
-
-
-
- CREATE PROCEDURE [dbo].[sp_insert_file]
- @file_name NVARCHAR(MAX),
- @file_ext NVARCHAR(MAX),
- @file_base64 NVARCHAR(MAX)
- AS
- BEGIN
- /****** Script for SelectTopNRows command from SSMS ******/
- INSERT INTO [dbo].[tbl_file]
- ([file_name]
- ,[file_ext]
- ,[file_base6])
- VALUES
- (@file_name
- ,@file_ext
- ,@file_base64)
- END
-
- GO
Step 2
Create a new MVC web project and name it "ImgSaveDb".
Step 3
Open the "Views->Shared->_Layout.cshtml" file and replace following code in it i.e.:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>@ViewBag.Title</title>
- @Styles.Render("~/Content/css")
- @Scripts.Render("~/bundles/modernizr")
-
-
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" />
-
- </head>
- <body>
- <div class="navbar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- </div>
- </div>
- </div>
- <div class="container body-content">
- @RenderBody()
- <hr />
- <footer>
- <center>
- <p><strong>Copyright © @DateTime.Now.Year - <a href="http://wwww.asmak9.com/">Asma's Blog</a>.</strong> All rights reserved.</p>
- </center>
- </footer>
- </div>
-
- @*Scripts*@
- @Scripts.Render("~/bundles/jquery")
-
- @Scripts.Render("~/bundles/jqueryval")
- @Scripts.Render("~/bundles/bootstrap")
-
- @RenderSection("scripts", required: false)
- </body>
- </html>
In the above code, I have simply created a basic default layout page and linked the required libraries into it.
Step 4
Create a new "Helper_Code\Objects\ImgObj.cs" file and replace the following code in it i.e.:
-
-
-
-
-
-
-
- namespace ImgSaveDb.Helper_Code.Objects
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
-
-
-
- public class ImgObj
- {
- #region Properties
-
-
-
-
- public int FileId { get; set; }
-
-
-
-
- public string FileName { get; set; }
-
-
-
-
- public string FileContentType { get; set; }
-
- #endregion
- }
- }
In the above code, I have simply created an object class which will map my image file metadata from SQL database.
Step 5
Now, create a new "Models\ImgViewModel.cs" file and replace the following code in it i.e.:
-
-
-
-
-
-
-
- namespace ImgSaveDb.Models
- {
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Web;
- using Helper_Code.Objects;
-
-
-
-
- public class ImgViewModel
- {
- #region Properties
-
-
-
-
- [Required]
- [Display(Name = "Upload File")]
- public HttpPostedFileBase FileAttach { get; set; }
-
-
-
-
- public List<ImgObj> ImgLst { get; set; }
-
- #endregion
- }
- }
In the above code, I have created my view model which I will attach with my view. Here, I have created a HttpPostedFileBase type file attachment property which will capture uploaded image/file data from the end-user and image object type list property which will display a list of images that I have stored in my database.
Step 6
Create a new "Controllers\ImgController.cs" file and replace the following code in it i.e.
-
-
-
-
-
-
-
- namespace ImgSaveDb.Controllers
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Helper_Code.Objects;
- using Models;
-
-
-
-
- public class ImgController : Controller
- {
- #region Private Properties
-
-
-
-
- private db_imgEntities databaseManager = new db_imgEntities();
-
- #endregion
-
- #region Index view method.
-
- #region Get: /Img/Index method.
-
-
-
-
-
- public ActionResult Index()
- {
-
- ImgViewModel model = new ImgViewModel { FileAttach = null, ImgLst = new List<ImgObj>() };
-
- try
- {
-
- model.ImgLst = this.databaseManager.sp_get_all_files().Select(p => new ImgObj
- {
- FileId = p.file_id,
- FileName = p.file_name,
- FileContentType = p.file_ext
- }).ToList();
- }
- catch (Exception ex)
- {
-
- Console.Write(ex);
- }
-
-
- return this.View(model);
- }
-
- #endregion
-
- #region POST: /Img/Index
-
-
-
-
-
-
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult Index(ImgViewModel model)
- {
-
- string fileContent = string.Empty;
- string fileContentType = string.Empty;
-
- try
- {
-
- if (ModelState.IsValid)
- {
-
- byte[] uploadedFile = new byte[model.FileAttach.InputStream.Length];
- model.FileAttach.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
-
-
- fileContent = Convert.ToBase64String(uploadedFile);
- fileContentType = model.FileAttach.ContentType;
-
-
- this.databaseManager.sp_insert_file(model.FileAttach.FileName, fileContentType, fileContent);
- }
-
-
- model.ImgLst = this.databaseManager.sp_get_all_files().Select(p => new ImgObj
- {
- FileId = p.file_id,
- FileName = p.file_name,
- FileContentType = p.file_ext
- }).ToList();
- }
- catch (Exception ex)
- {
-
- Console.Write(ex);
- }
-
-
- return this.View(model);
- }
-
- #endregion
-
- #endregion
-
- #region Download file methods
-
- #region GET: /Img/DownloadFile
-
-
-
-
-
-
- public ActionResult DownloadFile(int fileId)
- {
-
- ImgViewModel model = new ImgViewModel { FileAttach = null, ImgLst = new List<ImgObj>() };
-
- try
- {
-
- var fileInfo = this.databaseManager.sp_get_file_details(fileId).First();
-
-
- return this.GetFile(fileInfo.file_base6, fileInfo.file_ext);
- }
- catch (Exception ex)
- {
-
- Console.Write(ex);
- }
-
-
- return this.View(model);
- }
-
- #endregion
-
- #endregion
-
- #region Helpers
-
- #region Get file method.
-
-
-
-
-
-
-
- private FileResult GetFile(string fileContent, string fileContentType)
- {
-
- FileResult file = null;
-
- try
- {
-
- byte[] byteContent = Convert.FromBase64String(fileContent);
- file = this.File(byteContent, fileContentType);
- }
- catch (Exception ex)
- {
-
- throw ex;
- }
-
-
- return file;
- }
-
- #endregion
-
- #endregion
- }
- }
In the above code, I have created a databaseManager private property which will allow me to access my SQL database via entity framework. Then, I have created "GetFile(...)" helper method which will convert my SQL database stored image file from base64 data format to byte data format. Then, I have created "DownloadFile(...)" method which will return image file stored in the SQL database base on the provided image file ID. Then, I have created GET "Index(...)" method which will retrieve the list of images data from SQL database and send it to the view page. Finally, I have created POST() "Index(...)" method which will receive the input image file from the end-user, then convert that file into a base64 data format and finally save the base64 data format file into the SQL database.
Step 7
Now, create a view "Views\Img\Index.cshtml" file and replace the following code in it i.e.
- @using ImgSaveDb.Models
-
- @model ImgSaveDb.Models.ImgViewModel
-
- @{
- ViewBag.Title = "ASP.NET MVC5: Upload Image into Database";
- }
-
-
- <div class="row">
- <div class="panel-heading">
- <div class="col-md-8">
- <h3>
- <i class="fa fa-file-text-o"></i>
- <span>ASP.NET MVC5: Upload Image into Database</span>
- </h3>
- </div>
- </div>
- </div>
-
- <br/>
-
- <div class="row">
- <div class="col-md-6 col-md-push-2">
- <section>
- @using (Html.BeginForm("Index", "Img", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
- {
- @Html.AntiForgeryToken()
-
- <div class="well bs-component">
- <br />
-
- <div class="row">
- <div class="col-md-12">
- <div class="col-md-8 col-md-push-2">
- <div class="input-group">
- <span class="input-group-btn">
- <span class="btn btn-default btn-file">
- Browse…
- @Html.TextBoxFor(m => m.FileAttach, new { type = "file", placeholder = Html.DisplayNameFor(m => m.FileAttach), @class = "form-control" })
- </span>
- </span>
- <input type="text" class="form-control" readonly>
- </div>
- @Html.ValidationMessageFor(m => m.FileAttach, "", new { @class = "text-danger custom-danger" })
- </div>
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-12">
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-offset-5 col-md-10">
- <input type="submit" class="btn btn-danger" value="Upload" />
- </div>
- </div>
- </div>
- }
- </section>
- </div>
- </div>
-
- <hr />
-
- <div class="row">
- <div class="col-md-offset-4 col-md-8">
- <h3>List of Imagess </h3>
- </div>
- </div>
-
- <hr />
-
- @if (Model.ImgLst != null &&
- Model.ImgLst.Count > 0)
- {
- <div class="row">
- <div class="col-md-offset-1 col-md-8">
- <section>
- <table class="table table-bordered table-striped">
- <thead>
- <tr>
- <th style="text-align: center;">Sr.</th>
- <th style="text-align: center;">Image Name</th>
- <th style="text-align: center;"></th>
- </tr>
- </thead>
-
- <tbody>
- @for (int i = 0; i < Model.ImgLst.Count; i++)
- {
- <tr>
- <td style="text-align: center;">@(i + 1)</td>
-
- <td style="text-align: center;">
- <div class="input-group" style="height:40px;">
- <i class="fa fa-2x fa-paperclip text-navy"></i>
- <a class="download-file1" href="@Url.Action("DownloadFile", "Img", new { fileId = @Model.ImgLst[i].FileId })" target="_blank">
- @Model.ImgLst[i].FileName
- </a>
- </div>
- </td>
-
- <td style="text-align: center;">
- <div>
- <img src="@Url.Action("DownloadFile", "Img", new { fileId = @Model.ImgLst[i].FileId })" width="100" height="100" />
- </div>
- </td>
- </tr>
- }
- </tbody>
- </table>
- </section>
- </div>
- </div>
- }
-
- @section Scripts
- {
- @*Scripts*@
- @Scripts.Render("~/bundles/bootstrap-file")
-
- @*Styles*@
- @Styles.Render("~/Content/Bootstrap-file/css")
- }
In the above code, I have created a simple view for uploading an image file to the server for storing it into the SQL database and then display the uploaded image files list. I have created bootstrap style file upload control and a table to display the list of uploaded images on the server in the SQL database.
Step 8
Now, execute the project and you will be able to see the following in action i.e.:
Conclusion
In this article, you learned about uploading of the image file into the database on ASP.NET MVC5 platform. You also learned how to convert the image file into the base64 data format. You also learned to convert base64 data format to byte data format. You also learned to retrieve image data stored in SQL server database as base64 and you learned about the advantages & disadvantages of storing image/file into the database.