Sunday, July 19, 2009

How to upload and play a video (ASP.NET)

So I was given the task of creating a video management system on my work's website. This simply means employees can upload a video, and then allow anybody to view the video.

Notice: This is way more simple than a final version would be, this is just to show the basics of getting it to work.

1. Add the ability to upload video:
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (!fileUploadDialog.HasFile) //Check if user has specified file
{
lblStatus.Text = "Must select a video file to upload";
return;
}
string virtualFilePath = "~/VideoDirectory/" + fileUploadDialog.FileName;

string physicalFilePath = Path.Combine(Server.MapPath("VideoDirectory"), fileUploadDialog.FileName);
fileUploadDialog.SaveAs(physicalFilePath);

SqlCommand cmd = new SqlCommand();
try
{
Guid vidId = Guid.NewGuid();
cmd.CommandText = "INSERT INTO Videos(VideoID, VideoPath) VALUES(@VideoID, @VideoPath)";
cmd.Parameters.AddWithValue("@VideoID", vidId);
cmd.Parameters.AddWithValue("@VideoPath", virtualFilePath);
cmd.Connection = new SqlConnection(YourConnectionString);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
lblStatus.Text = "File has been uploaded!";

}
catch(SqlException se)
{
//SQL error occured, so delete file in the file system
File.Delete(physicalFilePath);
lblStatus.Text = "A SQL error occured, file has not been uploaded.";
}
finally
{
cmd.Connection.Close();
}
}
}

2. Use a GridView to display the list of videos. Dynamically generate links to the video viewing page:
<asp:gridview id="gvVid" runat="server" autogeneratecolumns="False">
<columns>
<asp:templatefield headertext="Name">
<itemtemplate>
<%# getFileName(Eval("VideoPath").ToString()) %>
</itemtemplate>

</asp:templatefield>

<asp:templatefield headertext="Video Link">
<itemtemplate>
<asp:hyperlink navigateurl="" vid="{0}&quot;)">">
ID="HyperLink1" runat="server">[View Video]</asp:hyperlink>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>

protected void Page_Load(object sender, EventArgs e)
{

}
protected string getFileName(string path)
{
return System.IO.Path.GetFileNameWithoutExtension(path);
}
protected void btnRefresh_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(YourConnectionString);
cmd.CommandText = "SELECT VideoID, VideoPath FROM Videos";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(dt);
gvVid.DataSource = dt;
gvVid.DataBind();
}
catch
{ }

}

3. Setup the behind code for the video viewer page

string vpath = "";

if (Request.QueryString["vid"] != null && new Guid(Request.QueryString["vid"]) != Guid.Empty)
{
SqlCommand cmd = new SqlCommand("SELECT VideoPath FROM Videos WHERE VideoID = @VideoID");

cmd.Connection = new SqlConnection(YourConnectionString);
cmd.Parameters.AddWithValue("@VideoID", Request.QueryString["vid"]);
try
{
cmd.Connection.Open();
vpath = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();

}
catch { }
finally
{
if (cmd.Connection.State == ConnectionState.Open)
cmd.Connection.Close();
}

}
Label1.Text = vpath;
MediaPlayer1.MediaSource = vpath;
}

**MediaPlayer1 is from Silverlight. You'll need to download this and include it in your project. Get silverlight here: silverlight sdk download

Now you have the basic setup that will allow you to create your own video management system. Good luck.

No comments:

Post a Comment

There was an error in this gadget