First Create Table
Create table Videos
(
ID Int identity,
Video varbinary(MAX),
Video_Name nvarchar(50),
Video_Size bigint
)
1.
Open Vs 2010 –> File –> New –> Website.
2.
select –> C#
3.
Select ‘Asp.Net Empty Website’ –> name – ‘Video’.
default.aspx
<%@ 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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1"
runat="server"
/>
<br />
<br />
<asp:Button ID="Button1"
runat="server"
onclick="Button1_Click"
Text="Upload"
/>
<br />
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
byte[] buffer;
SqlConnection con;
protected void
Button1_Click(object sender, EventArgs e)
{
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);
if (bytesReaded > 0)
{
try
{
SqlConnection con = new SqlConnection("Data Source=ABC;Initial
Catalog=db;Integrated Security=True");
SqlCommand cmd = new SqlCommand
("INSERT
INTO Videos (Video, Video_Name, Video_Size)" +
" VALUES (@video, @videoName,
@videoSize)", con);
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 (con)
{
con.Open();
int i = cmd.ExecuteNonQuery();
Label1.Text = "uploaded, " + i.ToString();
}
}
catch (Exception
ex)
{
Label1.Text = ex.Message.ToString();
}
}
}
else
{
Label1.Text = "Choose a valid video
file";
}
}
}
No comments:
Post a Comment