Uploading image or any type of file format in whether a file or into a database is always a topic of debate and confusion. Both the choices come with their set of advantages and disadvantages without a doubt. But, the ultimate decision entirely depends on the business requirement, resource costing, and availability.
Today, I shall be demonstrating the uploading of an image as a file on ASP.NET MVC5 platform. This article is not specific to image file only, you can use the provided solution with any type of file format as well.

Advantages (Pros)
- Storage capacity is not expensive as third-party file cloud storage servers can be utilized which may or may not charge an additional cost to meet your requirement.
- No additional code is needed to access the uploaded file.
- The performance to retrieve image/file via file path is much faster as compared to the decoding of base 64 code back to the image from the database.
- The image file can be directly edited via available jQuery plugins, such as - cropping and resizing tools for the image file.
- The database will have less load on it which improves the costing plans.
- The Web Server bandwidth is more likely not to be increased and the costing plan will be further improved.
Disadvantages (Cons)
- Sensitive images or any other file format data is not fully secured even when you use third-party cloud storage like Amazon S3 as a link to the file is always public in order to get accessed. So, if the unauthorized user somehow gets access to the direct link of the file, then, he/she can easily download it.
- Storing of uploaded files are not guaranteed in a sense that file link is broken/lost or file is not uploaded but a link is available.
- Extra backups of the files are required along with the backup of the database where file paths are stored.
- File integrity is not guaranteed because the developer might forget to delete the actual file and only delete the file link from the database. This enables not only consistency issue especially in the distributed environment with many replication servers, but, also threatens end-user privacy & trust in a sense that user is under the impression that his/her file is deleted as the system shows but, in reality, only the link is deleted and actual file exist on the system, which means that targeted organization can illegally use end-user files for any sort of activity without the end-user consent. So, there is no way for end-user to know if it's an actual unintentional bug in the system or intentional scam from the targeted organization to collect user file data. Remember Facebook image deletion scandal back in 2009 for reference (Facebook reference is used for only education/understanding purpose without the intention of harming the reputation of the organization).
- Dealing with file and path synchronization in a distributed environment is difficult especially with multiple replication servers for backup.
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
- USE [db_img]
- GO
- /****** Object: StoredProcedure [dbo].[sp_insert_file] Script Date: 11/19/2018 8:11:10 AM ******/
- DROP PROCEDURE [dbo].[sp_insert_file]
- GO
- /****** Object: StoredProcedure [dbo].[sp_get_file_details] Script Date: 11/19/2018 8:11:10 AM ******/
- DROP PROCEDURE [dbo].[sp_get_file_details]
- GO
- /****** Object: StoredProcedure [dbo].[sp_get_all_files] Script Date: 11/19/2018 8:11:10 AM ******/
- DROP PROCEDURE [dbo].[sp_get_all_files]
- GO
- /****** Object: Table [dbo].[tbl_file] Script Date: 11/19/2018 8:11:10 AM ******/
- DROP TABLE [dbo].[tbl_file]
- GO
- /****** Object: Table [dbo].[tbl_file] Script Date: 11/19/2018 8:11:10 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_path] [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/19/2018 8:11:10 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- =============================================
- -- Author: <Author,,Name>
- -- Create date: <Create Date,,>
- -- Description: <Description,,>
- -- =============================================
- 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/19/2018 8:11:10 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- =============================================
- -- Author: <Author,,Name>
- -- Create date: <Create Date,,>
- -- Description: <Description,,>
- -- =============================================
- 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_path]
- FROM [db_img].[dbo].[tbl_file]
- WHERE [tbl_file].[file_id] = @file_id
- END
- GO
- /****** Object: StoredProcedure [dbo].[sp_insert_file] Script Date: 11/19/2018 8:11:10 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- =============================================
- -- Author: <Author,,Name>
- -- Create date: <Create Date,,>
- -- Description: <Description,,>
- -- =============================================
- CREATE PROCEDURE [dbo].[sp_insert_file]
- @file_name NVARCHAR(MAX),
- @file_ext NVARCHAR(MAX),
- @file_path NVARCHAR(MAX)
- AS
- BEGIN
- /****** Script for SelectTopNRows command from SSMS ******/
- INSERT INTO [dbo].[tbl_file]
- ([file_name]
- ,[file_ext]
- ,[file_path])
- VALUES
- (@file_name
- ,@file_ext
- ,@file_path)
- END
- GO
Step 2
Step 3
- <!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")
- <!-- Font Awesome -->
- <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 require libraries into it.
Step 4
- //-----------------------------------------------------------------------
- // <copyright file="ImgObj.cs" company="None">
- // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.
- // </copyright>
- // <author>Asma Khalid</author>
- //-----------------------------------------------------------------------
- namespace MVCImageSaveFile.Helper_Code.Objects
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- /// <summary>
- /// Image object class.
- /// </summary>
- public class ImgObj
- {
- #region Properties
- /// <summary>
- /// Gets or sets Image ID.
- /// </summary>
- public int FileId { get; set; }
- /// <summary>
- /// Gets or sets Image name.
- /// </summary>
- public string FileName { get; set; }
- /// <summary>
- /// Gets or sets Image extension.
- /// </summary>
- 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
- //-----------------------------------------------------------------------
- // <copyright file="ImgViewModel.cs" company="None">
- // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.
- // </copyright>
- // <author>Asma Khalid</author>
- //-----------------------------------------------------------------------
- namespace MVCImageSaveFile.Models
- {
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Web;
- using Helper_Code.Objects;
- /// <summary>
- /// Image view model class.
- /// </summary>
- public class ImgViewModel
- {
- #region Properties
- /// <summary>
- /// Gets or sets Image file.
- /// </summary>
- [Required]
- [Display(Name = "Upload File")]
- public HttpPostedFileBase FileAttach { get; set; }
- /// <summary>
- /// Gets or sets Image file list.
- /// </summary>
- 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 HttpPostedFileBase type file attachment property which will capture the uploaded image/file data from the end-user and image object type list property which will display the list of images that I have stored as a file on my server and stored their file paths in my database.
Step 6








Md Sohel RanaPosted Dec 17, 2018, 11:06 PM
This article helpful
Vaibhav AgarwalPosted Dec 14, 2018, 8:28 PM
It would have been a great article if you would have also written about database upload for images along with Path Images.
Paras SinghPosted Dec 11, 2018, 7:39 AM
Good article
Alice NguyenPosted Dec 9, 2018, 7:39 AM
So great an Article
Rehman ShahidPosted Dec 9, 2018, 7:04 AM
Nice Article......(y)