Riddhi Valecha

Riddhi Valecha

  • 443
  • 3.3k
  • 410.8k

Save And Play Video Files on Web Page from Database

Mar 1 2017 7:36 AM
Dear all,
 
I have a requirement - I have to save a video file to SQL Server and Play it on Web Page.
 
1. Save Video on SQL Server database  table - done - varbinary(max) datatype.
2. Load Video details on Gridview. - Done.
3. Play Video - How to do this ??
 
In database - I have -
1. ID - int primary key
2. File Name - varchar(max)
3. Data - varbinary(max)
 
I have loaded ID and File Name on grid view... How to play video on web page?
 
ASPX File -  <form id="form1" runat="server">

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnupload" runat="server" Text="Upload" OnClick="btnupload_Click" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" Font-Names = "Arial" Font-Size = "10pt"
HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="Name" HeaderText="FileName" />
<asp:TemplateField>
<ItemTemplate>
<object type="application/x-shockwave-flash" data='dewplayer-vol.swf?mp3=FileCS.ashx?ID=<%# Eval("ID") %>'
width="240" height="20" id="dewplayer">
<param name="wmode" value="transparent" />
<param name="movie" value='dewplayer-vol.swf?mp4=FileHandler.ashx?ID=<%# Eval("ID") %>'/>
</object>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="Id" Text = "Download" DataNavigateUrlFormatString = "~/FileHandler.ashx?ID={0}" HeaderText="Download" />
</Columns>
</asp:GridView>
</form>
-------
ASPX.cs file -
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
namespace VideoInWeb
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select ID, Name from tblfiles";
cmd.Connection = con;
con.Open();
// DataList1.DataSource = cmd.ExecuteReader();
// DataList1.DataBind();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}

protected void btnupload_Click(object sender, EventArgs e)
{
using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
{
byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
cmd.Parameters.AddWithValue("@ContentType", "audio/mpeg3");
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}


}
 
Please guide -
 

Answers (4)