locked
I Have a Problem that to Retrieve Video File (.MP4) From SQL Server 2012 DataBase and which has to Play on Media Player. RRS feed

  • Question

  • User-1282930768 posted

    I Have a Problem that to Retrieve Video File (.MP4)  From SQL Server 2012 DataBase and which has to Play on Media Player.

    In this i am going to upload a Video in DataBase and Display the Link in Listview, and by clicking the Link the Video have to play FOR EXAMPLE : YOUTUBE 

    PLZ  Help Me any one .

    This is My Code

    YouTube_WebForm.aspx :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Data;
    using System.IO;

    public partial class YouTube_WebForm : System.Web.UI.Page
    {
          protected void Page_Load(object sender, EventArgs e)
         {
               if (!IsPostBack)
              {
                     BindGrid();
              } 
        }

    private void BindGrid()
    {
                string strConnString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strConnString))
               {
                     using (SqlCommand cmd = new SqlCommand())
                     {
                           cmd.CommandText = "select Video from TBL_YouTube";
                           cmd.Connection = con;
                           con.Open();
                          Listviewid.DataSource = cmd.ExecuteReader();
                          Listviewid.DataBind();
                          con.Close();
                     }
               }
    }


    protected void uploadbtn_Click(object sender, EventArgs e)
    {
                string k = FileUploadbtn.PostedFile.FileName.ToString();
                using (BinaryReader br = new BinaryReader(FileUploadbtn.PostedFile.InputStream))
                {
                         byte[] bytes = br.ReadBytes((int)FileUploadbtn.PostedFile.InputStream.Length);
                         string strConnString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
                        using (SqlConnection con = new SqlConnection(strConnString))
                        { 
                                using (SqlCommand cmd = new SqlCommand())
                                {
                                         cmd.CommandText = "insert into TBL_YouTube(ID, FileType, Video) values (@ID, @FileType, @Video)";
                                         cmd.Parameters.AddWithValue("@ID",FileUploadbtn.PostedFile.FileName.ToString() + DateTime.Now.ToLongDateString().ToString());
                                         cmd.Parameters.AddWithValue("@FileType", "video/mp4");
                                         cmd.Parameters.AddWithValue("@Video", bytes);
                                         cmd.Connection = con;
                                         con.Open();
                                         cmd.ExecuteNonQuery();
                                        con.Close();
                                }
                       }
               }
    Response.Redirect(Request.Url.AbsoluteUri);
    }

    protected void LinkID_Click(object sender, EventArgs e)
    {
              LinkButton linkplayvideo = sender as LinkButton;
              this.aPlayer.Visible = true;
             this.aPlayer.HRef = linkplayvideo.CommandArgument;

    }
    }

    YouTube_WebForm.aspx

    <body>
               <form id="form1" runat="server">
                          <div>
                                   <asp:FileUpload ID="FileUploadbtn" runat="server" CssClass="myButton" />
                                   <asp:Button ID="uploadbtn" runat="server" Text="Upload" CssClass="myButton" OnClick="uploadbtn_Click" />
                                  <asp:ListView ID="Listviewid" Visible="true" runat="server" AutoGenerateColumns="false" RepeatColumns="1" CellSpacing="15">
                                  <ItemTemplate>
                                                <table>
                                                            <tr>
                                                                     <td>
                                                                            <asp:LinkButton ID="LinkID" runat="server" Text="Play" CommandArgument='<%# Eval("Video", "Generic_Handler.ashx?Id={0}")%>' OnClick="LinkID_Click"></asp:LinkButton>
                                                                     </td>
                                                            </tr>
                                                  </table>
                               </ItemTemplate>
         </asp:ListView>
         <a class="player" id="aPlayer" runat="server" visible="false" style="height: 300px;  width: 300px; display: block;"></a>
         <script src="FlowPlayer/flowplayer-3.2.12.min.js" class="player" type="text/javascript"></script>
         <script type="text/javascript">

        flowplayer("a.player", "FlowPlayer/flowplayer-3.2.16.swf",
       {
             plugins:
             {
                 pseudo:
                   {
                         url: "FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf"
                   }
              },
       clip:
       {
            provider: 'pseudo', autoPlay: false
       },
    });
    </script>

    </div>
    </form>
    </body>

    DataBase:

    ID : varchar(MAX)  ----> In ID have to store FileName and Upload Current DataTime

    FileType : Varchar(50)

    Video : varbinary(MAX)

    Generic Handler.ashx

    <%@ WebHandler Language="C#" class="Generic_Handler" %>

    using System;
    using System.Web;
    using System.Data.SqlClient;
    using System.Configuration;

    public class Generic_Handler : IHttpHandler 
    {
        public void ProcessRequest (HttpContext context) 
        {
             int id = int.Parse(context.Request.QueryString["ID"]);
             byte[] bytes;
            string FileType;
            string strConnString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                     cmd.CommandText = "select Video, FileType from TBL_YouTube where ID=@ID";
                     cmd.Parameters.AddWithValue("@ID", id);
                     cmd.Connection = con;
                     con.Open();
                    SqlDataReader sdr = cmd.ExecuteReader();
                    sdr.Read();
                    bytes = (byte[])sdr["Video"];
                    FileType = sdr["FileType"].ToString();
                    con.Close();
               }
        }
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.AppendHeader("Content-Disposition", "attachment; ID=" + id);
       context.Response.ContentType = FileType;
       context.Response.BinaryWrite(bytes);
       context.Response.End();
      }

    public bool IsReusable
    {
         get 
        {
             return false;
        }
    }

    }

    In this Video is not Playing

    Thanks in Advance

    Regards Deepak

    Friday, April 17, 2015 2:21 AM

Answers

  • User1711366110 posted

    In this Video is not Playing

       As per this case, you have to use WHILE LOOP for retrieve the video file by using sqlDataReader.
    for your reference, check the following code :

    public void ProcessRequest (HttpContext context) 
         {
              int id = int.Parse(context.Request.QueryString["ID"]);
              byte[] bytes;
             string FileType;
             string strConnString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
             using (SqlConnection con = new SqlConnection(strConnString))
             {
                 using (SqlCommand cmd = new SqlCommand())
                 {
                      cmd.CommandText = "select Video, FileType from TBL_YouTube where ID=@ID";
                      cmd.Parameters.Add ("@ID", SqlDbType.Int).Value= id;
                      cmd.Connection = con;
                      con.Open();
                     SqlDataReader sdr = cmd.ExecuteReader (CommandBehavior.Default); 
                     if (sdr.HasRows)
                     {
                      while (sdr.Read())
                      {
                        bytes = (byte[])sdr["Video"];
                        FileType = sdr["FileType"].ToString();}
                      }
                    }
                    context.Response.Buffer = true;
                    context.Response.AppendHeader("Content-Disposition", "attachment; ID=" + id);
                    context.Response.ContentType = FileType;
                    context.Response.BinaryWrite(bytes);
                    context.Response.End();
                    con.Close();
              }
          }
    }

    --
    with regards,
    Edwin

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2015 4:41 AM