如何使用c# .net 3.5和sqlserver 2008在网页中显示视频
本文关键字:2008 网页 视频 显示 sqlserver 何使用 net | 更新日期: 2023-09-27 18:08:35
亲爱的朋友,我们有代码上传视频文件到数据库和工作良好。
我们也有代码在网页上显示在线视频,但当我们点击视频文件时,它不显示。当我们点击视频文件下载时,IDM(internet下载管理器)会自动开始下载。
我无法理解,请有人可以建议我们如何才能顺利显示我的视频文件,请
在我上传视频的代码下面。
数据库表CREATE TABLE [dbo].[tblVideos](
[id] [int] IDENTITY(1,1) NOT NULL,
[Video_Name] [varchar](50) NULL,
[Name] [varchar](50) NULL,
[ContentType] [varchar](50) NULL,
[Data] [varbinary](max) NULL
) ON [PRIMARY]
Aspx代码上传视频
<table style="margin-left:150px; height:546px">
<tr class="tr">
<td colspan="3">
<asp:Label ID="Label1" runat="server"
Font-Size="15pt" ForeColor="#0099FF" Text="Add New Video"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Font-Names="MS Reference Sans Serif"
Font-Size="7pt" Text="Make Sure Video in MP4 format"></asp:Label>
</td>
</tr>
<tr class="tr">
<td class="style1"> Video Name :</td>
<td><asp:TextBox ID="txtideoname" Height="24px" Width="270px" runat="server"></asp:TextBox></td>
<td>
</td>
</tr>
<tr class="tr">
<td class="style1">Video type :</td>
<td><asp:TextBox ID="txtvideotype" Height="24px" Width="270px" runat="server"></asp:TextBox></td>
<td>
</td>
</tr>
<tr class="tr">
<td class="style1">video:</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<td>
</td>
</tr>
<tr class="tr">
<td> </td>
<td><asp:Button ID="btnupload" runat="server" Text="Upload" Width="132px" Height="34px" BackColor="Red" BorderStyle="None" Font-Bold="True" ForeColor="White" onclick="btnupload_Click"/><br />
<asp:Label ID="lblMsg" runat="server"/></td>
<td>
</td>
</tr>
<tr class="tr">
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
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["con1"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "insert into tblVideos(Video_Name,Name, ContentType, Data) values (@Video_Name,@Name, @ContentType, @Data)";
cmd.Parameters.AddWithValue("@Video_Name", txtideoname.Text);
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
cmd.Parameters.AddWithValue("@ContentType", "video/mp4");
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
显示视频的aspx代码
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<hr />
<asp:DataList ID="DataList1" Visible="true" runat="server" AutoGenerateColumns="false"
RepeatColumns="2" CellSpacing="5">
<ItemTemplate>
<u>
<%# Eval("Name") %></u>
<hr />
<a class="player" style="height: 300px; width: 300px; display: block" href='<%# Eval("Id", "FileCS.ashx?Id={0}") %>'>
</a>
</ItemTemplate>
</asp:DataList>
<script src="FlowPlayer/flowplayer-3.2.12.min.js" type="text/javascript"></script>
<script type="text/javascript">
flowplayer("a.player", "FlowPlayer/flowplayer-3.2.16.swf", {
plugins: {
pseudo: { url: "FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf" }
},
clip: { provider: 'pseudo', autoPlay: false},
});
</script>
cs显示视频代码
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Id, Name from tblVideos";
cmd.Connection = con;
con.Open();
ddlvideogallery.DataSource = cmd.ExecuteReader();
ddlvideogallery.DataBind();
con.Close();
}
}
}
显示视频的通用处理器代码
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
sing System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Configuration;
namespace Mindpower
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class FileCs : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id = int.Parse(context.Request.QueryString["id"]);
byte[] bytes;
string contentType;
string strConnString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
string name;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, Data, ContentType from tblVideos where id=@id";
cmd.Parameters.AddWithValue("@id", id);
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
name = sdr["Name"].ToString();
con.Close();
}
}
context.Response.Clear();
context.Response.Buffer = true;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
context.Response.ContentType = contentType;
context.Response.BinaryWrite(bytes);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
请任何人给一些想法或解决方案
谢谢
您已在ihttphandler
中指定下载为附件context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);