Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to upload and display the video ..

i did this using the code below but ,its not working can anybody help me.

plz check the code below...thanx


database
ID	Int
Video	varbinary(MAX)
Video_Name	nvarchar(50)
Video_Size	bigint


Default.aspx
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="UploadVideo.ascx" tagname="UploadVideo" tagprefix="uc1" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    
        <uc1:UploadVideo ID="UploadVideo1"  runat="server" />
    
    </div>
    <br />
    <br />
    <asp:Button ID="ButtonShowVideo" runat="server" onclick="ButtonShowVideo_Click" 
        Text="Show Video" />
    <br />
    <asp:Repeater ID="Repeater1" runat="server">
        <itemtemplate>
            <object id="player" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" 
                        height="170" width="300">
                        <param name="url" value="<%# "VideoHandler.ashx?id=" + Eval("ID") %>" />
                        <param name="showcontrols" value="true" />
                        <param name="autostart" value="true" />
                    </object>
        </itemtemplate>
    
    <br />
    <br />
    <br />
    <asp:Button ID="ButtonBind" runat="server" onclick="ButtonBind_Click" 
        Text="Bind" />
    <asp:GridView ID="GridView1" runat="server" BackColor="White" 
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        ForeColor="Black" GridLines="Vertical">
        <footerstyle backcolor="#CCCC99" />
        <rowstyle backcolor="#F7F7DE" />
        <columns>
            <asp:templatefield>
                <itemtemplate>
                    <object id="player" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" 
                        height="170" width="300">
                        <param name="url" value="<%# "VideoHandler.ashx?id=" + Eval("ID") %>" />
                        <param name="showcontrols" value="true" />
                        <param name="autostart" value="true" />
                    </object>
                </itemtemplate>
            
        </columns>
        <pagerstyle backcolor="#F7F7DE" forecolor="Black" horizontalalign="Right" />
        <selectedrowstyle backcolor="#CE5D5A" font-bold="True" forecolor="White" />
        <headerstyle backcolor="#6B696B" font-bold="True" forecolor="White" />
        <alternatingrowstyle backcolor="White" />
    
    </form>
</body>
</html>


Default.aspx.cs
C#
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 _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonBind_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = GetVideoInfo();
        GridView1.DataBind();
    }

    private DataTable GetVideoInfo()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["uploadConnectionString"].ConnectionString;
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Videos", connectionString);
        DataTable table = new DataTable();
        adapter.Fill(table);
        return table;
    }

    private DataTable GetSpecificVideo(object i)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["uploadConnectionString"].ConnectionString;
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT Video, ID FROM Videos WHERE ID = @id", connectionString);
        adapter.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = (int)i;
        DataTable table = new DataTable();
        adapter.Fill(table);
        return table;
    }
    protected void ButtonShowVideo_Click(object sender, EventArgs e)
    {
        Repeater1.DataSource = GetSpecificVideo(2);
        Repeater1.DataBind();
    }
}


Uploadvideo.ascx
ASP.NET
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UploadVideo.ascx.cs" Inherits="UploadVideo" %>
<head>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<table class="style1">
    <tr>
        <td>
            <asp:FileUpload ID="FileUpload1" runat="server" />
        </td>
        <td>
             </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">
        </td>
    </tr>
    </table>


Uploadvideo.ascx.cs
C#
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 = ConfigurationManager.ConnectionStrings["uploadConnectionString"].ConnectionString;
                connection = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand
                    ("INSERT INTO Videos (Video, Video_Name, Video_Size) VALUES (@video, @videoName, @videoSize)", connection);
                cmd.Parameters.Add("@video", SqlDbType.VarBinary, buffer.Length).Value = buffer;
                cmd.Parameters.Add("@videoName", SqlDbType.NVarChar).Value = FileUpload1.FileName;
                cmd.Parameters.Add("@videoSize", 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";
        }
    }
}


VideoHandler.ashx
C#
<%@ WebHandler Language="C#" Class="VideoHandler" %>

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

public class VideoHandler : IHttpHandler 
{
    
    public void ProcessRequest (HttpContext context) 
    {
        string connectionString = ConfigurationManager.ConnectionStrings["uploadConnectionString"].ConnectionString;

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SELECT Video, Video_Name FROM Videos WHERE ID = @id", connection);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = context.Request.QueryString["id"];
        try
        {
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    context.Response.ContentType = reader["Video_Name"].ToString();
                    context.Response.BinaryWrite((byte[])reader["Video"]);
                }
            }
        }
        finally
        {
            connection.Close();
        }
    }
 
    public bool IsReusable 
    {
        get {
            return false;
        }
    }

}
Posted
v3
Comments
TrushnaK 30-Apr-13 2:08am    
Which error you getting?
Dixit Ashish 30-Apr-13 2:35am    
Mr. Trushak when i click to the upload button nothing will happen there..no error occurred..
Thanks7872 30-Apr-13 2:11am    
Dont post codes like this.Nobody would like to go through this type of code and you wont get much response.only post the code you suspect that have error.use improve question link at the bottom of the question and remove the unnecessary snippet.
TrushnaK 30-Apr-13 2:57am    
Debug your code to check your button click event firing or not ... and get back with error you getting.

Even me too having the same Problem .. i have uploaded in Database and retrieved too but the video is not displaying
 
Share this answer
 
you must send your byte array to a memorystram and then create video file from memory stream!


this cod is for create image from database:
C#
//dt is datatable
byte[] arrPicture = (byte[])(dt.Rows[0][0]);//my first column is varBinary
System.IO.MemoryStream ms = new System.IO.MemoryStream(arrPicture);
System.Drawing.Image myimage = System.Drawing.Image.FromStream(ms);


you can use similar this to create video file!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900