Wednesday, September 4, 2013

Video upload on Server and Play with the help of Video-light box using C# asp.net,Jquery and SQL server

Hello friends,
 Today I will be explain with you, how we can upload video on server and play on our website as our requirement using c# asp.net, Sql Server and flash player. Now I will be explaining all steps one by one. And you can also download flash file and player with all project source code from here.
 Step 1: Design database ,table and stroeprocedure.
CREATE TABLE [dbo].[Videolightbox](
      [videoid] [int] IDENTITY(1,1) NOT NULL,
      [videoname] [nvarchar](50) NULL,      [videopath] [nvarchar](150) NULL
) ON [PRIMARY]

And store procedure is
Create PROCEDURE Usp_Videolightbox
      -- Add the parameters for the stored procedure here
@Action Nvarchar(50)='',
@Id int=0,
@Videoname nvarchar(50)='',
@Path nvarchar(150)=''
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for procedure here
if(@Action='insertvideo')
begin
insert into Videolightbox (videoname,videopath) values(@Videoname,@Path)
end
if(@Action='getvideo')
begin
select videoid,videoname,'../../data/video/' +videopath as video from dbo.Videolightbox
end
END

Step 2: Now we will open our Visual Studio software and create a new page default.aspx, code of this is given below-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!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 id="Head1" runat="server">
    <title></title> <link href="engine_video/css/overlay-minimal.css" rel="stylesheet" type="text/css" />
    <link href="engine_video/css/videolightbox.css" rel="stylesheet" type="text/css" />
    <script src="engine_video/js/jquery.tools.min.js" type="text/javascript"></script>
    <script src="engine_video/js/swfobject.js" type="text/javascript"></script>
    <script src="engine_video/js/videolightbox.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <fieldset style=" width:500px"><legend>Video Lightbox</legend>
    <table><tr><td>Enter Video name</td><td><asp:TextBox ID="txtvideoname" runat="server"></asp:TextBox></td></tr>
   <tr><td>upload video</td><td><asp:FileUpload ID ="file1" runat="server" /></td></tr>
   <tr><td colspan="2" align="center"><asp:Button ID="cmdsave" runat="server"
           Text="Save" onclick="cmdsave_Click" /></td></tr>
   </table></fieldset>
   <br /><br />
   <fieldset style=" width:500px"><legend>Paly Video</legend><table style=" width:600px; border:1px solid #d5d5d5;"><tr style=" background:#d5d5d5;"><td>Video Name</td><td>Play Video</td></tr>
   <tr><td colspan="2"><asp:Repeater ID="repdata" runat="server"><ItemTemplate><table style=" width:500px; border-bottom:1px solid #d5d5d5;">
  
   <tr><td><asp:Label ID="lblvideo" runat="server" Text='<%#Eval("videoname")%>'></asp:Label></td><td align="center"><div id="videogallery">
       <a rel="#voverlay" href="engine_video/swf/player.swf?url=<%#Eval("video")%>&volume=100" title='<%#Eval("videoname")%>' >
   <img src="data/thumbnails/play-button.png" alt='<%#Eval("videoname")%>'  /><span></span></a><a id="videolb" href="#"></a>
       </div></td></tr></table>
  
   </ItemTemplate></asp:Repeater></td></tr></table></fieldset>
    </form>
</body>
</html>

And cs page of this page default.cs is-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adpt = new SqlDataAdapter();
    Hashtable hst = new Hashtable();
    DataTable dt = new DataTable();
    DataSet ds = new DataSet();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        chatconvertation();
    }
    public DataSet GetDataSet(string spName, Hashtable hst)
    {
        con = new SqlConnection();
        cmd = new SqlCommand();
        con.ConnectionString = constr;
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = spName;
        con.Open();
        if (hst != null)
            if (hst.Count > 0)
                AttachParameters(cmd, hst);
        DataSet dataset = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = cmd;
        adapter.Fill(dataset);
        con.Close();
        return dataset;
    }
    private void AttachParameters(SqlCommand command, Hashtable hstParameters)
    {
        try
        {
            IEnumerator parameters = hstParameters.Keys.GetEnumerator();
            while (parameters.MoveNext())
            {
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@" + parameters.Current;
                param.Value = hstParameters[parameters.Current];
                command.Parameters.Add(param);
            }
        }
        catch (Exception exception)
        {
            HttpContext.Current.Response.Write("Error : " + exception.Message + " </br> Source : " + exception.Source);
        }
    }
    public void chatconvertation()
    {
        hst.Clear();
        ds.Clear();
        hst.Add("Action", "getvideo");
        ds = GetDataSet("Usp_Videolightbox", hst);

        if (ds.Tables[0].Rows.Count > 0)
        {
            repdata.DataSource = ds;
            repdata.DataBind();
        }
    }
    protected void cmdsave_Click(object sender, EventArgs e)
    {
        hst.Clear();
        ds.Clear();
        string str1="";
         string FileName = string.Empty, sValidImage = string.Empty, sExe = string.Empty, sSavePath = string.Empty;
        if(file1.HasFile)
        {
             FileName = DateTime.Now.Day + "_" + DateTime.Now.Month + "_" + DateTime.Now.Year + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second + "_" + DateTime.Now.Millisecond + "_" + DateTime.Now.Ticks;

                sExe = System.IO.Path.GetExtension(file1.FileName.ToUpper());
                {
                    if (!Directory.Exists(Server.MapPath("data/video")))
                        Directory.CreateDirectory(Server.MapPath("data/video"));
                    sSavePath = "data/video/" + FileName + sExe;
                    //Session["Psth"] = sSavePath;
                    file1.SaveAs(Server.MapPath("~\\data\\video\\" + FileName + sExe));
                    str1 = FileName + sExe;
                }
        }
        hst.Add("Action", "insertvideo");
        hst.Add("Videoname", txtvideoname.Text);
        hst.Add("path",str1);
        ds=GetDataSet("Usp_Videolightbox",hst);
    }
}

This is the project for upload video on server and paly with help of video player. This is the screen shot of this application.





No comments:

Post a Comment