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.

ASP.NET MVC 5 - Upload Image/File Into Database

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)

  1. 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.
  2. Storing of the uploaded file is guaranteed.
  3. Images/Files shop on database do not require actress backups.
  4. 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.
  5. Epitome replication is piece of cake (if needed).
  6. 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)

  1. Database storage becomes expensive for many and large file storage.
  2. There will be a functioning penalty as latency to retrieve image/file is slower and database lookup is slower than filesystem lookup.
  3. Boosted code is needed to extract and stream files.
  4. 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.
  5. At that place will be more load on the database.
  6. Web server bandwidth will increase which adds additional costs.

Prerequisites

Following are some prerequisites before you proceed whatsoever farther in this tutorial:

  1. Cognition of ASP.Net MVC5.
  2. Knowledge of HTML.
  3. Knowledge of Bootstrap.
  4. 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.

  1. Apply [db_img]
  2. Become
  3. /****** Object:  StoredProcedure [dbo].[sp_insert_file]    ScriptEngagement : 11/18/2018 12:27:55 AM ******/
  4. DROP Process  [dbo].[sp_insert_file]
  5. Become
  6. /****** Object:  StoredProcedure [dbo].[sp_get_file_details]    ScriptDate : 11/18/2018 12:27:55 AM ******/
  7. Drib PROCEDURE  [dbo].[sp_get_file_details]
  8. GO
  9. /****** Object:  StoredProcedure [dbo].[sp_get_all_files]    ScriptAppointment : 11/18/2018 12:27:55 AM ******/
  10. DROP Procedure  [dbo].[sp_get_all_files]
  11. Become
  12. /****** Object:Tabular array  [dbo].[tbl_file]    Script Date : 11/18/2018 12:27:55 AM ******/
  13. DROP TABLE  [dbo].[tbl_file]
  14. Become
  15. /****** Object:Table  [dbo].[tbl_file]    Script Engagement : 11/xviii/2018 12:27:55 AM ******/
  16. Set up  ANSI_NULLS ON
  17. Go
  18. Set up  QUOTED_IDENTIFIER ON
  19. GO
  20. CREATE Tabular array  [dbo].[tbl_file](
  21.  [file_id] [int ] IDENTITY(one,1) Not NULL ,
  22.  [file_name] [nvarchar](max ) NOT NULL ,
  23.  [file_ext] [nvarchar](max ) Not NULL ,
  24.  [file_base6] [nvarchar](max ) Non NULL ,
  25. CONSTRAINT  [PK_tbl_file] Primary KEY  Clustered
  26. (
  27.  [file_id]ASC
  28. )WITH  (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON  [ PRIMARY ]
  29. )ON  [ PRIMARY ] TEXTIMAGE_ON [ Primary ]
  30. Go
  31. /****** Object:  StoredProcedure [dbo].[sp_get_all_files]    ScriptDate : 11/18/2018 12:27:55 AM ******/
  32. SET  ANSI_NULLS ON
  33. GO
  34. SET  QUOTED_IDENTIFIER ON
  35. GO
  36. CREATE PROCEDURE  [dbo].[sp_get_all_files]
  37. AS
  38. Begin
  39. /****** Scriptfor  SelectTopNRows command from  SSMS  ******/
  40. SELECT  [file_id]
  41.     ,[file_name]
  42.     ,[file_ext]
  43. FROM  [db_img].[dbo].[tbl_file]
  44. End
  45. Go
  46. /****** Object:  StoredProcedure [dbo].[sp_get_file_details]    ScriptEngagement : 11/eighteen/2018 12:27:55 AM ******/
  47. SET  ANSI_NULLS ON
  48. GO
  49. SET  QUOTED_IDENTIFIER ON
  50. GO
  51. CREATE Procedure  [dbo].[sp_get_file_details]
  52.  @file_idINT
  53. As
  54. Brainstorm
  55. /****** Scriptfor  SelectTopNRows control from  SSMS  ******/
  56. SELECT  [file_id]
  57.     ,[file_name]
  58.     ,[file_ext]
  59.     ,[file_base6]
  60. FROM  [db_img].[dbo].[tbl_file]
  61. WHERE  [tbl_file].[file_id] = @file_id
  62. Cease
  63. Go
  64. /****** Object:  StoredProcedure [dbo].[sp_insert_file]    ScriptDate : 11/18/2018 12:27:55 AM ******/
  65. Gear up  ANSI_NULLS ON
  66. Become
  67. Fix  QUOTED_IDENTIFIER ON
  68. GO
  69. CREATE Procedure  [dbo].[sp_insert_file]
  70.  @file_name NVARCHAR(MAX ),
  71.  @file_ext NVARCHAR(MAX ),
  72.  @file_base64 NVARCHAR(MAX )
  73. Every bit
  74. Begin
  75. /****** Scriptfor  SelectTopNRows control from  SSMS  ******/
  76. INSERT INTO  [dbo].[tbl_file]
  77.            ([file_name]
  78.            ,[file_ext]
  79.            ,[file_base6])
  80. VALUES
  81.            (@file_name
  82.            ,@file_ext
  83.            ,@file_base64)
  84. END
  85. 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.:

  1. <!DOCTYPE html >
  2. < html >
  3. < caput >
  4. < meta charset = "utf-viii" />
  5. < meta name = "viewport" content = "width=device-width, initial-calibration=1.0" >
  6. < title > @ViewBag.Championship </ title >
  7.     @Styles.Return("~/Content/css")
  8.     @Scripts.Render("~/bundles/modernizr")
  9. < link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-crawly/4.4.0/css/font-awesome.min.css" />
  10. </ head >
  11. < body >
  12. < div class = "navbar navbar-inverse navbar-fixed-top" >
  13. < div grade = "container" >
  14. < div class = "navbar-header" >
  15. < push type = "push" class = "navbar-toggle" data-toggle = "collapse" data-target = ".navbar-collapse" >
  16. < span course = "icon-bar" > </ span >
  17. < span class = "icon-bar" > </ span >
  18. < span class = "icon-bar" > </ bridge >
  19. </ push button >
  20. </ div >
  21. </ div >
  22. </ div >
  23. < div form = "container body-content" >
  24.         @RenderBody()
  25. < hour />
  26. < footer >
  27. < eye >
  28. < p > < strong > Copyright © @DateTime.Now.Year - < a href = "http://wwww.asmak9.com/" > Asma's Blog </ a > . </ stiff >  All rights reserved. </ p >
  29. </ center >
  30. </ footer >
  31. </ div >
  32.     @*Scripts*@
  33.     @Scripts.Render("~/bundles/jquery")
  34.     @Scripts.Render("~/bundles/jqueryval")
  35.     @Scripts.Return("~/bundles/bootstrap")
  36.     @RenderSection("scripts", required: fake)
  37. </ body >
  38. </ 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.:

  1. namespace  ImgSaveDb.Helper_Code.Objects
  2. {
  3. using  System;
  4. using  System.Collections.Generic;
  5. using  System.Linq;
  6. using  System.Web;
  7. public class  ImgObj
  8.     {
  9.         #region Backdrop
  10. public int  FileId { become ; set ; }
  11. public string  FileName { get ; set ; }
  12. public string  FileContentType { get ; set ; }
  13.         

0 Response to "Upload Image and Save to Database in Asp Mvc"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel