locked
stocker des videos dans une base de données RRS feed

  • Question

  • User-625406877 posted

    I need help to store videos in a database
    I have fields to add an operation
    nameoperation + videos (with a fileupload)
    how can i use this video to store it or to store the path or to store the name
    and I want in client page to display the video I want

    Sunday, July 22, 2018 10:33 AM

Answers

  • User283571144 posted

    Hi  medessabbahi,

    When we deal with audio files , we normally firstly save the file in the server and store  the file path in to database.

    If you want to show the video in another page , you only need to get the path of the video  and set the video's src attribute the path you have got.

    Below is my demo for dealing with video.

    <form id="form1" runat="server"  >
            <div>
        
                <asp:Label ID="Label2" runat="server" Text="VideoName"></asp:Label><asp:TextBox ID="VideoName" runat="server"></asp:TextBox><br/>
                <asp:Label ID="Label3" runat="server" Text="File"></asp:Label><asp:FileUpload ID="FileUpload1" runat="server" />
     
                <br />
                
                      <asp:Button ID="Button1" runat="server" Text="Button"  OnClick="Button1_Click" />
             
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/ShowPage.aspx">go to showPage</asp:LinkButton>
            </div>
     
          
        </form>
     
    

    Code behind.

    protected void Button1_Click(object sender, EventArgs e)
            {
     
                //get the posted file
                HttpPostedFile file = FileUpload1.PostedFile;
     
                // rename the file
                string name = "/File/" + Guid.NewGuid().ToString() + ".mp4";
                // save the file , you should call Server.MapPath method to get the absolute path of the file
                file.SaveAs(Server.MapPath( name));
                Label1.Text = name;
     
     
                //store the name and path in database
                string sql = "insert into Video (name,videopath) values(@name,@videoPath)";
                SqlParameter[] sqlParameters = new SqlParameter[] {
                    new SqlParameter("@name",System.Data.SqlDbType.Char,50){Value=VideoName.Text},
                    new SqlParameter("@videoPath",System.Data.SqlDbType.Char,100){Value=name}
                };
                string connstr = "data source=localhost;initial catalog=bjhksj;integrated security=True";
             
                    using (SqlConnection con = new SqlConnection(connstr))
                    {
                        using (SqlCommand com = new SqlCommand(sql, con))
                        {
                            
                            com.Parameters.AddRange(sqlParameters);
                            con.Open();
                            com.ExecuteNonQuery();
                        }
                    }
                
     
            }
     
    

    The page to show the video.

    <form id="form1" runat="server">
            <div>
     
                 <asp:Label ID="Label2" runat="server" Text="VideoName"></asp:Label> :<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br/>
                <div>
                 <asp:Label ID="Label4" runat="server" Text="VideoPath"></asp:Label>:<asp:Label ID="Label3" runat="server" Text="File"></asp:Label>
                   
     
                </div>
           
                <video  controls="controls" id="myVideo"></video>
                
                  
           
            </div>
           <script>
     
               $(function () {
    // get the path of the video to show and set the video's src attribute the path you have got
                   $("#myVideo").attr("src", $("#Label3").text());
               })
     
           </script>
    

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
     
                SqlConnection con = new SqlConnection("data source=localhost;initial catalog=bjhksj;integrated security=True");
     
       // get the data from database ,I only select the first one of the videos to demonstrate
                string sql = "select top 1 * from Video";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
     
     
                    con.Open();            
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            reader.Read();
                          Label1.Text=  reader.GetString(2);
                            Label3.Text = reader.GetString(1);
                        }
                    } 
     
                }
            }
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2018 6:19 AM

All replies

  • User-625406877 posted

    I need help to store videos in a database
    I have fields to add an operation
    nameoperation + videos (with a fileupload)
    how can i use this video to store it or to store the path or to store the name
    and I want in client page to display the video I want
    asp.net

    Sunday, July 22, 2018 12:10 PM
  • User-1171043462 posted

    What are you trying to say by pasting same thing again

    Sunday, July 22, 2018 12:19 PM
  • User283571144 posted

    Hi  medessabbahi,

    When we deal with audio files , we normally firstly save the file in the server and store  the file path in to database.

    If you want to show the video in another page , you only need to get the path of the video  and set the video's src attribute the path you have got.

    Below is my demo for dealing with video.

    <form id="form1" runat="server"  >
            <div>
        
                <asp:Label ID="Label2" runat="server" Text="VideoName"></asp:Label><asp:TextBox ID="VideoName" runat="server"></asp:TextBox><br/>
                <asp:Label ID="Label3" runat="server" Text="File"></asp:Label><asp:FileUpload ID="FileUpload1" runat="server" />
     
                <br />
                
                      <asp:Button ID="Button1" runat="server" Text="Button"  OnClick="Button1_Click" />
             
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/ShowPage.aspx">go to showPage</asp:LinkButton>
            </div>
     
          
        </form>
     
    

    Code behind.

    protected void Button1_Click(object sender, EventArgs e)
            {
     
                //get the posted file
                HttpPostedFile file = FileUpload1.PostedFile;
     
                // rename the file
                string name = "/File/" + Guid.NewGuid().ToString() + ".mp4";
                // save the file , you should call Server.MapPath method to get the absolute path of the file
                file.SaveAs(Server.MapPath( name));
                Label1.Text = name;
     
     
                //store the name and path in database
                string sql = "insert into Video (name,videopath) values(@name,@videoPath)";
                SqlParameter[] sqlParameters = new SqlParameter[] {
                    new SqlParameter("@name",System.Data.SqlDbType.Char,50){Value=VideoName.Text},
                    new SqlParameter("@videoPath",System.Data.SqlDbType.Char,100){Value=name}
                };
                string connstr = "data source=localhost;initial catalog=bjhksj;integrated security=True";
             
                    using (SqlConnection con = new SqlConnection(connstr))
                    {
                        using (SqlCommand com = new SqlCommand(sql, con))
                        {
                            
                            com.Parameters.AddRange(sqlParameters);
                            con.Open();
                            com.ExecuteNonQuery();
                        }
                    }
                
     
            }
     
    

    The page to show the video.

    <form id="form1" runat="server">
            <div>
     
                 <asp:Label ID="Label2" runat="server" Text="VideoName"></asp:Label> :<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br/>
                <div>
                 <asp:Label ID="Label4" runat="server" Text="VideoPath"></asp:Label>:<asp:Label ID="Label3" runat="server" Text="File"></asp:Label>
                   
     
                </div>
           
                <video  controls="controls" id="myVideo"></video>
                
                  
           
            </div>
           <script>
     
               $(function () {
    // get the path of the video to show and set the video's src attribute the path you have got
                   $("#myVideo").attr("src", $("#Label3").text());
               })
     
           </script>
    

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
     
                SqlConnection con = new SqlConnection("data source=localhost;initial catalog=bjhksj;integrated security=True");
     
       // get the data from database ,I only select the first one of the videos to demonstrate
                string sql = "select top 1 * from Video";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
     
     
                    con.Open();            
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            reader.Read();
                          Label1.Text=  reader.GetString(2);
                            Label3.Text = reader.GetString(1);
                        }
                    } 
     
                }
            }
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2018 6:19 AM