Upload Image and Save to Database in Asp Mvc
Collecting image information or any other file format data is equally important as collecting textual information. Although collecting textual data is more simple than collecting an epitome or whatever 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 ane's business & system requirement.
Today, I shall exist demonstrating uploading of the image file into the database on ASP.Internet MVC5 platform. This article is non specific to image files only, you can apply 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 information into the database.
Advantages (Pros)
- Sensitive images or whatsoever other file format data is fully secure as it is only accessible via organization/software. Files are non attainable via links.
- Storing of the uploaded file is guaranteed.
- Images/Files shop on database do not require actress backups.
- Transnational integrity is guaranteed as yous won't be locked into typical reader/author problem and deleting the entry in the database means the file is really deleted, you practise not demand extra precautions to delete the file i.eastward. ensuring that file is deleted from both database and file system.
- Epitome replication is piece of cake (if needed).
- In load-balanced spider web servers or distributed environments, dealing with synchronizing images across multiple file systems is hard specially in a spider web application where a new server may exist added whatever fourth dimension.
Disadvantages (Cons)
- Database storage becomes expensive for many and large file storage.
- There will be a functioning penalty as latency to retrieve image/file is slower and database lookup is slower than filesystem lookup.
- Boosted code is needed to extract and stream files.
- You cannot straight edit the files, especially images files, as you can not direct employ image edit features such equally file resize and file cropping.
- At that place will be more load on the database.
- Web server bandwidth will increase which adds additional costs.
Prerequisites
Following are some prerequisites before you proceed whatsoever farther in this tutorial:
- Cognition of ASP.Net MVC5.
- Knowledge of HTML.
- Knowledge of Bootstrap.
- Cognition of C# Programming.
You tin can download the complete source lawmaking for this tutorial or you can follow the step by step discussion below. The sample code is existence developed in Microsoft Visual Studio 2015 Enterprise.
Let'due south begin now.
Step one
First, create your SQL server database and name it "db_img". Then execute following script into your SQL server database i.e.
- Apply [db_img]
- Become
- /****** Object: StoredProcedure [dbo].[sp_insert_file] ScriptEngagement : 11/18/2018 12:27:55 AM ******/
- DROP Process [dbo].[sp_insert_file]
- Become
- /****** Object: StoredProcedure [dbo].[sp_get_file_details] ScriptDate : 11/18/2018 12:27:55 AM ******/
- Drib PROCEDURE [dbo].[sp_get_file_details]
- GO
- /****** Object: StoredProcedure [dbo].[sp_get_all_files] ScriptAppointment : 11/18/2018 12:27:55 AM ******/
- DROP Procedure [dbo].[sp_get_all_files]
- Become
- /****** Object:Tabular array [dbo].[tbl_file] Script Date : 11/18/2018 12:27:55 AM ******/
- DROP TABLE [dbo].[tbl_file]
- Become
- /****** Object:Table [dbo].[tbl_file] Script Engagement : 11/xviii/2018 12:27:55 AM ******/
- Set up ANSI_NULLS ON
- Go
- Set up QUOTED_IDENTIFIER ON
- GO
- CREATE Tabular array [dbo].[tbl_file](
- [file_id] [int ] IDENTITY(one,1) Not NULL ,
- [file_name] [nvarchar](max ) NOT NULL ,
- [file_ext] [nvarchar](max ) Not NULL ,
- [file_base6] [nvarchar](max ) Non 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] ScriptDate : 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
- /****** Scriptfor 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] ScriptEngagement : 11/eighteen/2018 12:27:55 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE Procedure [dbo].[sp_get_file_details]
- @file_idINT
- As
- Brainstorm
- /****** Scriptfor SelectTopNRows control from SSMS ******/
- SELECT [file_id]
- ,[file_name]
- ,[file_ext]
- ,[file_base6]
- FROM [db_img].[dbo].[tbl_file]
- WHERE [tbl_file].[file_id] = @file_id
- Cease
- Go
- /****** Object: StoredProcedure [dbo].[sp_insert_file] ScriptDate : 11/18/2018 12:27:55 AM ******/
- Gear up ANSI_NULLS ON
- Become
- Fix QUOTED_IDENTIFIER ON
- GO
- CREATE Procedure [dbo].[sp_insert_file]
- @file_name NVARCHAR(MAX ),
- @file_ext NVARCHAR(MAX ),
- @file_base64 NVARCHAR(MAX )
- Every bit
- Begin
- /****** Scriptfor SelectTopNRows control from SSMS ******/
- INSERT INTO [dbo].[tbl_file]
- ([file_name]
- ,[file_ext]
- ,[file_base6])
- VALUES
- (@file_name
- ,@file_ext
- ,@file_base64)
- END
- Become
Step 2
Create a new MVC web project and proper name it "ImgSaveDb".
Step 3
Open the "Views->Shared->_Layout.cshtml" file and supervene upon post-obit code in it i.e.:
- <!DOCTYPE html >
- < html >
- < caput >
- < meta charset = "utf-viii" />
- < meta name = "viewport" content = "width=device-width, initial-calibration=1.0" >
- < title > @ViewBag.Championship </ title >
- @Styles.Return("~/Content/css")
- @Scripts.Render("~/bundles/modernizr")
- < link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-crawly/4.4.0/css/font-awesome.min.css" />
- </ head >
- < body >
- < div class = "navbar navbar-inverse navbar-fixed-top" >
- < div grade = "container" >
- < div class = "navbar-header" >
- < push type = "push" class = "navbar-toggle" data-toggle = "collapse" data-target = ".navbar-collapse" >
- < span course = "icon-bar" > </ span >
- < span class = "icon-bar" > </ span >
- < span class = "icon-bar" > </ bridge >
- </ push button >
- </ div >
- </ div >
- </ div >
- < div form = "container body-content" >
- @RenderBody()
- < hour />
- < footer >
- < eye >
- < p > < strong > Copyright © @DateTime.Now.Year - < a href = "http://wwww.asmak9.com/" > Asma's Blog </ a > . </ stiff > All rights reserved. </ p >
- </ center >
- </ footer >
- </ div >
- @*Scripts*@
- @Scripts.Render("~/bundles/jquery")
- @Scripts.Render("~/bundles/jqueryval")
- @Scripts.Return("~/bundles/bootstrap")
- @RenderSection("scripts", required: fake)
- </ body >
- </ html >
In the above lawmaking, I have but created a basic default layout folio and linked the required libraries into it.
Step 4
Create a new "Helper_Code\Objects\ImgObj.cs" file and supervene upon the following code in it i.eastward.:
- namespace ImgSaveDb.Helper_Code.Objects
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- public class ImgObj
- {
- #region Backdrop
- public int FileId { become ; set ; }
- public string FileName { get ; set ; }
- public string FileContentType { get ; set ; }
0 Response to "Upload Image and Save to Database in Asp Mvc"
Post a Comment