Wednesday 13 March 2013

Upload and Play MP4 Video files from Database in ASP.Net using C#




Description:-
                  

     in this example we explain that how to upload video in database same like we upload image in database. you can simply upload video by choosing video file and simply click on upload button. you can add video up to 5mb in database.

You can Upload any type of video like. mov, .avi, .wav,.flv etc.. and also play this video retrieves from the database. In this example a FileUpload control save the MP4 Video files to database and an ASP.Net DataList control to display the uploaded video files with it’s name link like display youtube video and also allows the user to play the MP4 Video file by just clicking on the play button. User can also paly and pause the video and also increase and decrease the volume of video same like as we have seen in youtube video player.user can also move the next and previous video file.

In which we are used  a Generic Handler which will fetch the uploaded MP4 video files through it’s Id from the database table and will act as a live stream source for the Flash Video Player.we can also used javascript for navigation of video button and display the player for video in which we play the video.


To upload video in database First you have to Create a Table in DataBase like



We also Create a webcontrol class throuch which we can easily upload video in DataBase.First you can upload video in DataBase then you can also play this video File.

To Play Uploaded Video in Asp.net please Click Here How to Play Video in Asp.Net

To Show Example of How to Upload File in MVC Application then click here upload Image and bind to Gridview in MVC

To show Example of How to Upload Multiple File in MVC Application then click here upload multiple File in MVC

How to upload File and Fetch file from SqlServer and bind to Gridview fetch file from Sqlserver and bind to Gridview


upload.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
<%@ Register TagPrefix="uc1" TagName="UploadVideo" Src="UploadVideo.ascx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


<uc1:UploadVideo ID="UploadVideo1" runat="server" />
  

</asp:Content>




 upload.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    private DataTable GetVideoInfo()
    {
        string connectionString = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM video", connectionString);
        DataTable table = new DataTable();
        adapter.Fill(table);
        return table;
    }

    private DataTable GetSpecificVideo()
    {
        string connectionString = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT ID FROM video", connectionString);
        //adapter.SelectCommand.Parameters.Add("@ID", SqlDbType.Int).Value = (int)i;
        DataTable table = new DataTable();
        adapter.Fill(table);
        return table;
    }

}






 UploadVideo.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UploadVideo.ascx.cs" Inherits="UploadVideo" %>
<head>
<title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<table class="style1">
    <tr>
        <td>
            <asp:FileUpload ID="FileUpload1" runat="server" />
        </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="ButtonUpload" runat="server" onclick="ButtonUpload_Click"
                Text="Upload" />
        </td>
        <td width="50%">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </td>
    </tr>
    </table>





 UploadVideo.ascx.cs


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class UploadVideo : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    byte[] buffer;//this is the array of bytes which will hold the data (file)
    SqlConnection connection;
    protected void ButtonUpload_Click(object sender, EventArgs e)
    {
        //check the file
        if (FileUpload1.HasFile && FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            HttpPostedFile file = FileUpload1.PostedFile;//retrieve the HttpPostedFile object
            buffer = new byte[file.ContentLength];
            int bytesReaded = file.InputStream.Read(buffer, 0, FileUpload1.PostedFile.ContentLength);
            //the HttpPostedFile has InputStream porperty (using System.IO;)
            //which can read the stream to the buffer object,
            //the first parameter is the array of bytes to store in,
            //the second parameter is the zero index (of specific byte) where to start storing in the buffer,
            //the third parameter is the number of bytes you want to read (do u care about this?)
            if (bytesReaded > 0)
            {
                try
                {
                    string connectionString = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
                    connection = new SqlConnection(connectionString);
                    SqlCommand cmd = new SqlCommand
                        ("INSERT INTO video (Video, Video_Name, Video_Size) VALUES (@Video, @Video_Name, @Video_Size)", connection);
                    cmd.Parameters.Add("@Video", SqlDbType.VarBinary, buffer.Length).Value = buffer;
                    cmd.Parameters.Add("@Video_Name", SqlDbType.NVarChar).Value = FileUpload1.FileName;
                    cmd.Parameters.Add("@Video_Size", SqlDbType.BigInt).Value = file.ContentLength;
                    using (connection)
                    {
                        connection.Open();
                        int i = cmd.ExecuteNonQuery();
                        Label1.Text = "uploaded, " + i.ToString() + " rows affected";
                    }
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message.ToString();
                }
            }

        }
        else
        {
            Label1.Text = "Choose a valid video file";
        }
    }

}

2 comments:

  1. this is wrong as codeproject copy remove..here cmd connection itself not declared...remove man this copy

    ReplyDelete