Store the Image

Requirement:

  • Create a Directory dynamically, if it does not exist.
  • Store the Images to the Folder.
  • Store the Image Path to the Database.

Database Table

  1. create table ImageUpload
  2. (
  3. Sr_No int identity,
  4. Pictures varchar(500)
  5. )
Complete Design (Source View) .aspx file
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Image_Saving_To_SQL_Using_ServerMapPath.aspx.cs" Inherits="Image_Saving_To_SQL" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. <style type="text/css">
  4. .Heading
  5. {
  6. font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; font-style: normal;
  7. font-variant: normal; text-transform: capitalize; color: #808080;
  8. text-align:center;
  9. height:30px;
  10. padding-top:15px;
  11. margin:10px;
  12. background-color:Gray;
  13. }
  14. .Content
  15. {
  16. margin:10px;
  17. height:400px;
  18. }
  19. .style1
  20. {
  21. width: 545px;
  22. }
  23. .style2
  24. {
  25. width: 151px;
  26. }
  27. .style3
  28. {
  29. width: 126px;
  30. }
  31. </style>
  32. </asp:Content>
  33. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  34. <div style="height: 783px">
  35. <div class="Heading">
  36. Saving Image To Database</div>
  37. <div class="Content">
  38. <br />
  39. <table style="width:100%;">
  40. <tr>
  41. <td class="style3">
  42. </td>
  43. <td class="style1">
  44. <asp:Image ID="Image1" runat="server" Height="293px" Width="584px" />
  45. </td>
  46. <td>
  47. </td>
  48. </tr>
  49. <tr>
  50. <td class="style3">
  51. </td>
  52. <td class="style1">
  53. <asp:FileUpload ID="FileUpload1" runat="server" />
  54. <asp:Button ID="btnUpload" runat="server" BackColor="#009933" Font-Bold="True"
  55. ForeColor="White" Height="30px" onclick="btnUpload_Click" Text="Upload"
  56. Width="100px" />
  57. <asp:Button ID="btnSave" runat="server" BackColor="#009900" Font-Bold="True"
  58. ForeColor="White" Height="30px" onclick="btnSave_Click" Text="Save"
  59. Width="100px" />
  60. </td>
  61. <td>
  62. </td>
  63. </tr>
  64. <tr>
  65. <td class="style3">
  66. </td>
  67. <td class="style1">
  68. <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
  69. </td>
  70. <td>
  71. </td>
  72. </tr>
  73. </table>
  74. </div>
  75. </div>
  76. </asp:Content>
C# Code (aspx.cs) file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Data.SqlTypes;
  10. using System.Drawing;
  11. using System.Configuration;
  12. //
  13. using System.IO; //Include this namespace
  14. public partial class Image_Saving_To_SQL : System.Web.UI.Page
  15. {
  16. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString());
  17. string ImageStr;
  18. string OldImg;
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. if (!IsPostBack)
  22. {
  23. }
  24. }
  25. protected void btnUpload_Click(object sender, EventArgs e)
  26. {
  27. try
  28. {
  29. if (FileUpload1.HasFile)
  30. {
  31. string dirUrl = "Uploads" + this.Page.User.Identity.Name;
  32. string dirPath = Server.MapPath(dirUrl);
  33. // Check for Directory, If not exist, then create it
  34. if (!Directory.Exists(dirPath))
  35. {
  36. Directory.CreateDirectory(dirPath);
  37. }
  38. // save the file to the Specifyed folder
  39. string fileUrl = dirUrl + "/" + Path.GetFileName(FileUpload1.PostedFile.FileName);
  40. FileUpload1.PostedFile.SaveAs(Server.MapPath(fileUrl));
  41. //Display the Image in the File Upload Control
  42. Image1.ImageUrl = fileUrl;
  43. }
  44. }
  45. catch (Exception Exc)
  46. {
  47. Label1.ForeColor = Color.Red;
  48. Label1.Text = "Application Error : " + Exc.Message;
  49. }
  50. }
  51. protected void btnSave_Click(object sender, EventArgs e)
  52. {
  53. try
  54. {
  55. con.Open();
  56. SqlCommand cmd = new SqlCommand("select Pictures from ImageUpload where Pictures='"+Image1.ImageUrl+"' ", con);
  57. SqlDataReader dr = cmd.ExecuteReader();
  58. if (dr.Read())
  59. {
  60. // Check, if the Image is available in the Database
  61. OldImg = dr["Pictures"].ToString();
  62. if (OldImg == Image1.ImageUrl)
  63. {
  64. ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('This Image Already Exist.')", true);
  65. }
  66. }
  67. else
  68. {
  69. // If not avaliable, then insert it path to the database
  70. dr.Close();
  71. SqlCommand cmd1 = new SqlCommand("insert into ImageUpload values('" + Image1.ImageUrl + "')", con);
  72. cmd1.ExecuteNonQuery();
  73. }
  74. con.Close();
  75. }
  76. catch (Exception Exc)
  77. {
  78. Label1.ForeColor = Color.Red;
  79. Label1.Text = "Application Error." + Exc.Message;
  80. }
  81. }
  82. }
Run the Application

  • Upload some Photo
  • See the Output.

Upload
Watch the Database, Table.

Watch the Database

Thank You.