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
<%@ 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
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
<%@ 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
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;
SqlConnection connection;
protected void ButtonUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile && FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
HttpPostedFile file = FileUpload1.PostedFile;
buffer = new byte[file.ContentLength];
int bytesReaded = file.InputStream.Read(buffer, 0, FileUpload1.PostedFile.ContentLength);
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
<%@ 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;
}
}
}