Create Directory Dynamically and Save Image to Folder in ASP.NET

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

 

  • Upload some Photo
  • See the Output.

Upload
Watch the Database, Table.

Watch the Database

Thank You.