【发布时间】:2013-12-09 00:29:53
【问题描述】:
有人要求我使用 C# 在 Visual Studio 2010 中创建一个网站,我需要在其中上传、下载和播放视频文件,很像 youtube。我已经完成了上传部分,但我无法从数据库部分下载和播放视频。这是我一直在下载和播放页面上使用的代码:
表格详情:
Vid_Id (int), Video_Name (nvarchar), 视频(VarBinary), Video_Size(大整数), 添加(nvarchar)
Default.aspx
<asp:Panel ID="Panel1" runat="server" Height="363px">
<asp:DataList ID="Repeater1" runat="server" DataSourceID="dsvidz">
<ItemTemplate>
<video id="player" height="300" width="300" controls autoplay>
<source src='<%# "VideoHandler.ashx?id=" + Eval("Vid_Id") + "&type=mp4" %>' type='video/mp4'/>
</video>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Download Video"
onclick="Button1_Click" Enabled="False" />
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.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
public partial class Default : System.Web.UI.Page
{
SqlConnection connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True");
private int id = 0, index=0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
private DataTable GetSpecificVideo(object i)
//pass the id of the video
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT Video, Vid_Id " + "FROM Vid_Dir WHERE Vid_Id = @id", connection);
adapter.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = (int)i;
DataTable table = new DataTable();
adapter.Fill(table);
return table;
}
void BindData()
{
//hfSearchText has the search string returned from the grid.
if (hfSearchText.Value != "")
dsvidz.SelectCommand += " where " + hfSearchText.Value;
DataView dv = (DataView)dsvidz.Select(new DataSourceSelectArguments());
//hfSort has the sort string returned from the grid.
if (hfSort.Value != "")
dv.Sort = hfSort.Value;
GridView1.DataSource = dv;
try
{
GridView1.DataBind();
}
catch (Exception exp)
{
//If databinding threw exception bcoz current page index is > than available page index
GridView1.PageIndex = 0;
GridView1.DataBind();
}
finally
{
//Select the first row returned
if (GridView1.Rows.Count > 0)
GridView1.SelectedIndex = 0;
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "play")
{
this.index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[this.index];
SqlCommand cmd1 = new SqlCommand("SELECT Vid_Id FROM Vid_Dir WHERE Video_Name=@VN", connection);
SqlDataReader idRdr = null;
idRdr = cmd1.ExecuteReader();
while (idRdr.Read())
{
this.id = Convert.ToInt32(idRdr["Vid_Id"]);
Label1.Text = Convert.ToString(idRdr["Video_Name"]);
Label3.Text = Convert.ToString(idRdr["Added"]);
Label5.Text = Convert.ToString(idRdr["Description"]);
}
Repeater1.DataSource = GetSpecificVideo(this.id);
Repeater1.DataBind();
Button1.Enabled=true;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable file = GetAFile(this.id);
DataRow row = file.Rows[this.index];
string name = (string)row["Video_Name"];
Byte[] data = (Byte[])row["Video"];
// Send the file to the browser
Response.AddHeader("Content-type", "video/mp4");
Response.AddHeader("Content-Disposition", "attachment; filename=" + name);
Response.BinaryWrite(data);
Response.Flush();
Response.End();
}
public static DataTable GetAFile(int i)
{
DataTable file = new DataTable();
using (SqlConnection connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandTimeout = 0;
cmd.CommandText = "SELECT Vid_Id, Video_Name, Video_Size FROM Vid_Dir " + "WHERE Vid_Id=@ID";
cmd.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter();
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters["@ID"].Value = i;
adapter.SelectCommand = cmd;
adapter.Fill(file);
connection.Close();
}
return file;
}
}
VideoHandler.ashx
<%@ WebHandler Language="C#" Class="VideoHandler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
public class VideoHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
SqlConnection connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand("SELECT Video, Video_Name" + " FROM Vid_Dir WHERE Vid_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;
}
}
}
此处使用的 HTML5 视频标签显示错误消息:
来源无效
但是,当我在运行时通过右键单击它从播放器中复制 url,然后将其粘贴到浏览器地址栏并按 Enter 时,它开始下载名称为
的视频视频处理程序.mp4
此外,它显示的视频播放器数量与数据库中上传的视频数量相同。 谁能帮帮我吗..? 谢谢
【问题讨论】:
-
请删除所有不相关的代码。例如,CSS 与问题有什么关系?
-
删除了不相关的代码。正如你所说。
标签: c# asp.net sql visual-studio-2010 sql-server-express