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.
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 as a file.
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
First, create your SQL Server database and name it as "db_img". Then, execute the following script into your SQL Server database.
- 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
-
-
-
-
-
- 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
-
-
-
-
-
- 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
-
-
-
-
-
- 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
Create a new MVC web project and name it as "MVCImageSaveFile".
Step 3
Open the "Views->Shared->_Layout.cshtml" file and replace the code with the following code in it.
- <!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 require libraries into it.
Step 4
Create a new "Helper_Code\Objects\ImgObj.cs" file and paste the following code in it.
-
-
-
-
-
-
-
- namespace MVCImageSaveFile.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 put the following code in that.
-
-
-
-
-
-
-
- namespace MVCImageSaveFile.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 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
Create a new "Controllers\ImgController.cs" file and add the following code.
-
-
-
-
-
-
-
- namespace MVCImageSaveFile.Controllers
- {
- using System;
- using System.Collections.Generic;
- using System.IO;
- 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 filePath = 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);
-
-
- fileContentType = model.FileAttach.ContentType;
- string folderPath = "~/Content/upload_files/";
- this.WriteBytesToFile(this.Server.MapPath(folderPath), uploadedFile, model.FileAttach.FileName);
- filePath = folderPath + model.FileAttach.FileName;
-
-
- this.databaseManager.sp_insert_file(model.FileAttach.FileName, fileContentType, filePath);
- }
-
-
- 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_path);
- }
- catch (Exception ex)
- {
-
- Console.Write(ex);
- }
-
-
- return this.View(model);
- }
-
- #endregion
-
- #endregion
-
- #region Helpers
-
- #region Get file method.
-
-
-
-
-
-
- private FileResult GetFile(string filePath)
- {
-
- FileResult file = null;
-
- try
- {
-
- string contentType = MimeMapping.GetMimeMapping(filePath);
-
-
- file = this.File(filePath, contentType);
- }
- catch (Exception ex)
- {
-
- throw ex;
- }
-
-
- return file;
- }
-
- #endregion
-
- #region Write to file
-
-
-
-
-
-
-
- private void WriteBytesToFile(string rootFolderPath, byte[] fileBytes, string filename)
- {
- try
- {
-
- if (!Directory.Exists(rootFolderPath))
- {
-
- string fullFolderPath = rootFolderPath;
-
-
- string folderPath = new Uri(fullFolderPath).LocalPath;
-
-
- Directory.CreateDirectory(folderPath);
- }
-
-
- string fullFilePath = rootFolderPath + filename;
-
-
- FileStream fs = System.IO.File.Create(fullFilePath);
-
-
- fs.Flush();
- fs.Dispose();
- fs.Close();
-
-
- BinaryWriter sw = new BinaryWriter(new FileStream(fullFilePath, FileMode.Create, FileAccess.Write));
-
-
- sw.Write(fileBytes);
-
-
- sw.Flush();
- sw.Dispose();
- sw.Close();
- }
- catch (Exception ex)
- {
-
- throw ex;
- }
- }
-
- #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 a "GetFile(...)" helper method which will return the image file from my server base on the image file path stored in my SQL database.
- I have also created the "WriteBytesToFile(...)" helper method which will store an uploaded file into my server provided file content path, which, in my case, is "~/Content/upload_files/".
- Then, I have created "DownloadFile(...)" method which will return image file stored in the SQL database base on the provided image file ID.
- I have created GET "Index(...)" method which will retrieve the list of images metadata from SQL database and send it to the View page.
- Finally, I have created a POST "Index(...)" method which will receive the input image file from the end-user, then store that image file into my server at "~/Content/upload_files/" file location as a file using "WriteBytesToFile(...)" helper method. Then, it will store the file metadata and the file path into the SQL database.
Step 7
Now, create a View "Views\Img\Index.cshtml" file and add the following code.
- @using MVCImageSaveFile.Models
-
- @model MVCImageSaveFile.Models.ImgViewModel
-
- @{
- ViewBag.Title = "ASP.NET MVC5: Upload Image as File";
- }
-
-
- <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 as File</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 and to store the file path into the SQL database and then display the list of uploaded image files. I have created a bootstrap style file upload control and a table to display the list of uploaded images on the server.
Step 8
Now, execute the project and you will be able to see the following in action.
Before file uploading, your "~/Content/upload_files/" server folder will be empty.
After the files are uploaded, it will contain the image files.
Now, type "http://{your_site_url}/Content/upload_files/no-img.png" URL in your browser and you will be able to see the following.
This means you can access the files as long as you know the link. It doesn't matter whether the user is logged into the system or not.
Conclusion
In this article, we learned about uploading of images as files on ASP.NET MVC5 platform. You also learned to store image/file on your server in a fixed folder. Not only this, we saw the advantages & disadvantages of storing image/files as files.