locked
**How to create folder dynamically using File upload server control**?? RRS feed

  • Question

  • User-1205636946 posted

     Hi Everyone,

     I have a Question regarding File upload server control.I want to Organize my Images according to date and company name.

     Let me explain my scenario here in detail.Plz take some time to read.

     

     I have Menu tab at the top of my webform like this:- HOME |ABOUT US |CONTACT| JOB

     JOB tab  has sub menus:- JOB1, JOB2, JOB3.

     When user clicks on say JOB1, the link is redirected to the webform JOB1.aspx page.

     Within that form i have 3 TextBoxes(for name,age,address), 1 Dropdownlist , File upload server control and save button.

     My Dropdownlist contains list of company names(say IBM,MICROSOFT.ORACLE).

     I have also created a Folder named IMAGES in my Solution explorer.

     

    *** Ok Now My Real Question starts from here***.

     If user fills up the 3 textboxes, Choose a option from the dropdownlist, browse and uploads image(say image.gif),clicks save button

    Then i want the image to be saved in the IMAGES Folder in this way:-

    IMAGES(folder)>TODAYDATE(folder)>JOB1(folder)>COMPANYNAME(folder)>IMAGE.GIF

    Here all the folders are automatically created using code.(except IMAGES)

    For eg: IMAGES>28thNOV>JOB1>IBM>image.gif

    The value of 3 textboxes,dropdownlist,and the link to the images saved in the folder resides in the sqldatabase table

    If  he comes back tomorrow and fills up the form.The results will be like this.

               MAGES>29thNOV>JOB2>MICROSOFT>sunset.jpeg

     

    I hope i have explained it clearly.

    If you ask me why do i want to achieve this because i want to organize my images nicely according to the date and company name,

    so that i can search for the logos and files relating to the company more easily. 

    Any one Plz help me to achieve this through code in C# using vwd2005 express and sql express?

    Thanks.

    Jack.
     


     



     


      
     

    Thursday, February 28, 2008 5:44 AM

Answers

  • User-2115483147 posted

    Hi Jack,

    Base on my experience, the key to solve your problem is:

    1. How to create a Directory, see the following code:

          string subPath ="ImagesPath"; // your code goes here
            bool IsExists = System.IO.Directory.Exists(Server.MapPath(subPath));
            if(!IsExists)
                System.IO.Directory.CreateDirectory(Server.MapPath(subPath));

    2. How to access SQL database, see the following code:

    DB:

     
    CREATE TABLE [dbo].[News](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [title] [varchar](50) NULL,
    [news] [varchar](50) NULL,
    [imageurl] [varchar](20) NULL,
    [detail] [varchar](5000) NULL,
    CONSTRAINT [PK_News] PRIMARY KEY CLUSTERED
    (
    [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]
     
    Admin_News.aspx: 
     
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Page Language="C#" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">


    public static string SqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TakeawayConnectionString"].ToString();

    public static void ExecuteNonQuery(string queryString)
    {
    using (SqlConnection connection = new SqlConnection(SqlConnectionString))
    {
    SqlCommand command = new SqlCommand(queryString, connection);
    command.Connection.Open();
    command.ExecuteNonQuery();
    command.Connection.Close();
    }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
    StringBuilder sqlInsert = new StringBuilder();
    sqlInsert.Append("INSERT INTO [News]([title],[news],[imageurl],[detail])VALUES('");
    sqlInsert.Append(txtTitle.Text);
    sqlInsert.Append("','");
    sqlInsert.Append(txtNews.Text);
    sqlInsert.Append("','");
    sqlInsert.Append(txtImageURL.Text);
    sqlInsert.Append("','");
    sqlInsert.Append(txtDetail.Text);
    sqlInsert.Append("')");
    ExecuteNonQuery(sqlInsert.ToString());
    GridView1.DataBind();

    }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Admin_News</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
    DataSourceID="SqlDataSource1">
    <Columns>
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
    ShowSelectButton="True" />
    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
    ReadOnly="True" SortExpression="id" />
    <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
    <asp:BoundField DataField="news" HeaderText="news" SortExpression="news" />
    <asp:BoundField DataField="imageurl" HeaderText="imageurl"
    SortExpression="imageurl" />
    <asp:BoundField DataField="detail" HeaderText="detail"
    SortExpression="detail" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
    DeleteCommand="DELETE FROM [News] WHERE [id] = @id"
    InsertCommand="INSERT INTO [News] ([title], [news], [imageurl], [detail]) VALUES (@title, @news, @imageurl, @detail)"
    SelectCommand="SELECT * FROM [News]"
    UpdateCommand="UPDATE [News] SET [title] = @title, [news] = @news, [imageurl] = @imageurl, [detail] = @detail WHERE [id] = @id">
    <DeleteParameters>
    <asp:Parameter Name="id" Type="Int64" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:Parameter Name="title" Type="String" />
    <asp:Parameter Name="news" Type="String" />
    <asp:Parameter Name="imageurl" Type="String" />
    <asp:Parameter Name="detail" Type="String" />
    <asp:Parameter Name="id" Type="Int64" />
    </UpdateParameters>
    <InsertParameters>
    <asp:Parameter Name="title" Type="String" />
    <asp:Parameter Name="news" Type="String" />
    <asp:Parameter Name="imageurl" Type="String" />
    <asp:Parameter Name="detail" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>
    <br />
    <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtNews" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="txtImageURL" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtDetail" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add"
    Width="37px" />
    <br />
    <br />
    </div>
    </form>
    </body>
    </html>
      
    News.aspx: 
     
    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>News</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <HeaderTemplate>
    <table border="1">
    <tr>
    <td>
    <b>title</b>
    </td>
    <td>
    <b>news</b>
    </td>
    <td>
    <b>imges</b>
    </td>
    <td>
    <b>detail</b>
    </td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td>
    <%# DataBinder.Eval(Container.DataItem, "title")%>
    </td>
    <td>
    <%# DataBinder.Eval(Container.DataItem, "news")%>
    </td>
    <td>
    <img alt="" src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>' />
    </td>
    <td>
    <%# DataBinder.Eval(Container.DataItem, "detail")%>
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
    SelectCommand="SELECT [title], [news], [imageurl], [detail] FROM [News]"></asp:SqlDataSource>
    </div>
    </form>
    </body>
    </html>

     

    Web.Config:
      <connectionStrings>
          <add name="TakeawayConnectionString" connectionString="Data Source=ocean-chen;Initial Catalog=Takeaway;Integrated Security=True"
              providerName="System.Data.SqlClient" /> <!-- please change the connection string -->
      </connectionStrings>

    Let me know if I have misunderstood what you mean. Thanks.

    Hope it helps,

    Hong Gang


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2008 12:22 AM

All replies

  • User-2115483147 posted

    Hi Jack,

    Base on my experience, the key to solve your problem is:

    1. How to create a Directory, see the following code:

          string subPath ="ImagesPath"; // your code goes here
            bool IsExists = System.IO.Directory.Exists(Server.MapPath(subPath));
            if(!IsExists)
                System.IO.Directory.CreateDirectory(Server.MapPath(subPath));

    2. How to access SQL database, see the following code:

    DB:

     
    CREATE TABLE [dbo].[News](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [title] [varchar](50) NULL,
    [news] [varchar](50) NULL,
    [imageurl] [varchar](20) NULL,
    [detail] [varchar](5000) NULL,
    CONSTRAINT [PK_News] PRIMARY KEY CLUSTERED
    (
    [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]
     
    Admin_News.aspx: 
     
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Page Language="C#" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">


    public static string SqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TakeawayConnectionString"].ToString();

    public static void ExecuteNonQuery(string queryString)
    {
    using (SqlConnection connection = new SqlConnection(SqlConnectionString))
    {
    SqlCommand command = new SqlCommand(queryString, connection);
    command.Connection.Open();
    command.ExecuteNonQuery();
    command.Connection.Close();
    }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
    StringBuilder sqlInsert = new StringBuilder();
    sqlInsert.Append("INSERT INTO [News]([title],[news],[imageurl],[detail])VALUES('");
    sqlInsert.Append(txtTitle.Text);
    sqlInsert.Append("','");
    sqlInsert.Append(txtNews.Text);
    sqlInsert.Append("','");
    sqlInsert.Append(txtImageURL.Text);
    sqlInsert.Append("','");
    sqlInsert.Append(txtDetail.Text);
    sqlInsert.Append("')");
    ExecuteNonQuery(sqlInsert.ToString());
    GridView1.DataBind();

    }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Admin_News</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
    DataSourceID="SqlDataSource1">
    <Columns>
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
    ShowSelectButton="True" />
    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
    ReadOnly="True" SortExpression="id" />
    <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
    <asp:BoundField DataField="news" HeaderText="news" SortExpression="news" />
    <asp:BoundField DataField="imageurl" HeaderText="imageurl"
    SortExpression="imageurl" />
    <asp:BoundField DataField="detail" HeaderText="detail"
    SortExpression="detail" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
    DeleteCommand="DELETE FROM [News] WHERE [id] = @id"
    InsertCommand="INSERT INTO [News] ([title], [news], [imageurl], [detail]) VALUES (@title, @news, @imageurl, @detail)"
    SelectCommand="SELECT * FROM [News]"
    UpdateCommand="UPDATE [News] SET [title] = @title, [news] = @news, [imageurl] = @imageurl, [detail] = @detail WHERE [id] = @id">
    <DeleteParameters>
    <asp:Parameter Name="id" Type="Int64" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:Parameter Name="title" Type="String" />
    <asp:Parameter Name="news" Type="String" />
    <asp:Parameter Name="imageurl" Type="String" />
    <asp:Parameter Name="detail" Type="String" />
    <asp:Parameter Name="id" Type="Int64" />
    </UpdateParameters>
    <InsertParameters>
    <asp:Parameter Name="title" Type="String" />
    <asp:Parameter Name="news" Type="String" />
    <asp:Parameter Name="imageurl" Type="String" />
    <asp:Parameter Name="detail" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>
    <br />
    <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtNews" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="txtImageURL" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtDetail" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add"
    Width="37px" />
    <br />
    <br />
    </div>
    </form>
    </body>
    </html>
      
    News.aspx: 
     
    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>News</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <HeaderTemplate>
    <table border="1">
    <tr>
    <td>
    <b>title</b>
    </td>
    <td>
    <b>news</b>
    </td>
    <td>
    <b>imges</b>
    </td>
    <td>
    <b>detail</b>
    </td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td>
    <%# DataBinder.Eval(Container.DataItem, "title")%>
    </td>
    <td>
    <%# DataBinder.Eval(Container.DataItem, "news")%>
    </td>
    <td>
    <img alt="" src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>' />
    </td>
    <td>
    <%# DataBinder.Eval(Container.DataItem, "detail")%>
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TakeawayConnectionString %>"
    SelectCommand="SELECT [title], [news], [imageurl], [detail] FROM [News]"></asp:SqlDataSource>
    </div>
    </form>
    </body>
    </html>

     

    Web.Config:
      <connectionStrings>
          <add name="TakeawayConnectionString" connectionString="Data Source=ocean-chen;Initial Catalog=Takeaway;Integrated Security=True"
              providerName="System.Data.SqlClient" /> <!-- please change the connection string -->
      </connectionStrings>

    Let me know if I have misunderstood what you mean. Thanks.

    Hope it helps,

    Hong Gang


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2008 12:22 AM
  • User-1099836970 posted

    HI,

    This has been pretty usefull , but i am a fresher in this field so i have another doubt, i ve been using this code

     FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));

    for saving that file in a folder name upload , my question is "How to save that file in that newly created folder"
     

    Wednesday, March 19, 2008 1:10 AM