在ASP.NET网站中实现带列表的视频播放
方法:
用c#将视频文件上传到磁盘上时同时生产对应的xml文件,播放页面使用js读取对应的xml文件生成播放列表。
xml文件:
文件名:huojian.xml
<?xml version="1.0" encoding="utf-8" ?>
<media>
<pp url="http://s-sps:801/UploadedImages/badnewfish反坦克导弹.mpeg" name="badnewfish反坦克导弹" />
<pp url="http://s-sps:801/UploadedImages/badnewfish反坦克导弹.mpeg" name="badnewfish反坦克导弹" />
</media>
<media>
<pp url="http://s-sps:801/UploadedImages/badnewfish反坦克导弹.mpeg" name="badnewfish反坦克导弹" />
<pp url="http://s-sps:801/UploadedImages/badnewfish反坦克导弹.mpeg" name="badnewfish反坦克导弹" />
</media>
视频上传代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.Name != @"banewfish\banewfish")
{
Label1.Text = "您当前不具有当前网页的使用权限!<br/>如需上传文件请于管理员联系!";
try
{
FileUpload1.Enabled = false;
Button1.Enabled = false;
}
catch (Exception exDisenabledContorls)
{
FileUpload1.Enabled = false;
Button1.Enabled = false;
}
finally
{
FileUpload1.Enabled = false;
Button1.Enabled = false;
}
}
}
private string path;
protected void Button1_Click(object sender, EventArgs e)
{
string xmlDocName = dl.SelectedValue;
if (Page.IsPostBack)
{
Label1.Text = "";
Boolean fileOK = false;
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".wmv", ".mpg", ".mpeg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
else Label1.Text = "文件名不能为空";
if (fileOK)
{
path = Server.MapPath("~/UploadedImages/");
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
}
catch (Exception exSaveFile)
{
fileOK = false;
Label1.Text = "文件存储到服务器时出错,可能是文件已损坏或文件为空+" + exSaveFile.ToString();
}
//object[] pa = new object[] { FileUpload1.FileName.ToLower(),12, Page.User.Identity.Name.ToString() };
//Label1.Text += InsertToDataBase(@"Data Source=s-sql;Initial Catalog=sps;Integrated Security=True", pa);
if (fileOK)
{
Label1.Text += InsertToXML(xmlDocName);
}
}
else
{
Label1.Text = "文件类型错误,允许的文件类型为: wmv、mpg、mpeg";
}
}
//Page.Response.Write(file1.value);
}
private string InsertToXML(string xmlDocName)
{
XmlDocument xmlDoc = new XmlDocument();
string fileName = xmlDocName + ".xml";
try
{
xmlDoc.Load(Page.MapPath(fileName));
}
catch (Exception exLoadxml)
{
return xmlDocName;
}
XmlNode root = xmlDoc.SelectSingleNode("media");//查找<media>
XmlElement xe1 = xmlDoc.CreateElement("pp");//创建一个<pp>节点
xe1.SetAttribute("url","http://s-sps:801/UploadedImages/"+FileUpload1.FileName.ToLower());//设置该节点url属性
xe1.SetAttribute("name", FileUpload1.FileName.ToLower().Remove(FileUpload1.FileName.ToLower().LastIndexOf(".")));//设置该节点name属性
root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.PreserveWhitespace = true;
xmlDoc.Save(Page.MapPath(fileName));
return "上传成功";
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.Name != @"banewfish\banewfish")
{
Label1.Text = "您当前不具有当前网页的使用权限!<br/>如需上传文件请于管理员联系!";
try
{
FileUpload1.Enabled = false;
Button1.Enabled = false;
}
catch (Exception exDisenabledContorls)
{
FileUpload1.Enabled = false;
Button1.Enabled = false;
}
finally
{
FileUpload1.Enabled = false;
Button1.Enabled = false;
}
}
}
private string path;
protected void Button1_Click(object sender, EventArgs e)
{
string xmlDocName = dl.SelectedValue;
if (Page.IsPostBack)
{
Label1.Text = "";
Boolean fileOK = false;
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".wmv", ".mpg", ".mpeg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
else Label1.Text = "文件名不能为空";
if (fileOK)
{
path = Server.MapPath("~/UploadedImages/");
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
}
catch (Exception exSaveFile)
{
fileOK = false;
Label1.Text = "文件存储到服务器时出错,可能是文件已损坏或文件为空+" + exSaveFile.ToString();
}
//object[] pa = new object[] { FileUpload1.FileName.ToLower(),12, Page.User.Identity.Name.ToString() };
//Label1.Text += InsertToDataBase(@"Data Source=s-sql;Initial Catalog=sps;Integrated Security=True", pa);
if (fileOK)
{
Label1.Text += InsertToXML(xmlDocName);
}
}
else
{
Label1.Text = "文件类型错误,允许的文件类型为: wmv、mpg、mpeg";
}
}
//Page.Response.Write(file1.value);
}
private string InsertToXML(string xmlDocName)
{
XmlDocument xmlDoc = new XmlDocument();
string fileName = xmlDocName + ".xml";
try
{
xmlDoc.Load(Page.MapPath(fileName));
}
catch (Exception exLoadxml)
{
return xmlDocName;
}
XmlNode root = xmlDoc.SelectSingleNode("media");//查找<media>
XmlElement xe1 = xmlDoc.CreateElement("pp");//创建一个<pp>节点
xe1.SetAttribute("url","http://s-sps:801/UploadedImages/"+FileUpload1.FileName.ToLower());//设置该节点url属性
xe1.SetAttribute("name", FileUpload1.FileName.ToLower().Remove(FileUpload1.FileName.ToLower().LastIndexOf(".")));//设置该节点name属性
root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.PreserveWhitespace = true;
xmlDoc.Save(Page.MapPath(fileName));
return "上传成功";
}
}
视频播放模块代码:
<table cellspacing="0" cellpadding="4" width="100%" border="0">
<tbody>
<tr>
<td style="vertical-align: top; 70%">
<object id="pl" style=" 100%; height: 519px" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6"
viewastext="">
<param value="" name="URL" />
<param value="1" name="rate" />
<param value="0" name="balance" />
<param value="0" name="currentPosition" />
<param value="" name="defaultFrame" />
<param value="1" name="playCount" />
<param value="-1" name="autoStart" />
<param value="0" name="currentMarker" />
<param value="-1" name="invokeURLs" />
<param value="" name="baseURL" />
<param value="50" name="volume" />
<param value="0" name="mute" />
<param value="full" name="uiMode" />
<param value="0" name="stretchToFit" />
<param value="0" name="windowlessVideo" />
<param value="-1" name="enabled" />
<param value="-1" name="enableContextMenu" />
<param value="0" name="fullScreen" />
<param value="" name="SAMIStyle" />
<param value="" name="SAMILang" />
<param value="" name="SAMIFilename" />
<param value="" name="captioningID" />
<param value="0" name="enableErrorDialogs" />
<param value="6482" name="_cx" />
<param value="6350" name="_cy" />
</object>
</td>
<td style="vertical-align: top; 30%">
播放列表<br />
<script language="javascript" type="text/javascript">
// JScript 文件
function loadXML(xmlFile)
{
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load(xmlFile);
}
else
{
return null;
}
return xmlDoc;
}
//一个检查xml文件的函数
checkXMLDocObj = function(xmlFile)
{
var xmlDoc = loadXML(xmlFile);
if(xmlDoc==null)
{
alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!');
window.location.href='/Index.aspx';
}
return xmlDoc;
}
function doo()
{
// 定义一个读取xml文件的对象
var xmlDoc = checkXMLDocObj('http://s-sps:801/huojian.xml');
var name = new Array();
var url = new Array();
var sum=xmlDoc.getElementsByTagName("pp").length;
for(var i=0; i<sum; i++)
{
url[i]=xmlDoc.getElementsByTagName('media/pp')[i].getAttribute('url');
name[i]=xmlDoc.getElementsByTagName('media/pp')[i].getAttribute('name');
}
var str = new Array();
for(var i=0; i<sum; i++)
{
str[i]="return play('"+url[i]+"')";
}
document.write('<table border="0" cellpadding="0" cellspacing="0">');
for(var i=0; i<sum; i++)
{
var x = i+1;
document.write('<tr><td style="background-image: url(playbj.GIF);background-position:center;background-repeat:no-repeat; 20px;height:24px; text-align:center;vertical-align:middle;" >'+x+'</td><td>');
document.write('<input id=Button1 type=button value='+name[i].toString()+' onclick="'+str[i]+'" style="padding:2 0 0 2; border:0px;vertical-align:middle; background-color:#fff;text-align:left;">');
document.write('</td></tr>');
document.write('<tr><td style="height:1px;"><td><tr>');
}
document.write('</table>');
}
function play(urlx)
{
document.all.item("pl").url=urlx;
document.all.item("pl").controls.play();
}
doo();
</script>
</td>
</tr>
<tr>
<td id="_invisibleIfEmpty" style="display: none" valign="top" width="100%" colspan="2"
name="_invisibleIfEmpty">
</td>
</tr>
<script language="javascript"> if(typeof(MSOLayout_MakeInvisibleIfEmpty) == "function") {MSOLayout_MakeInvisibleIfEmpty();}</script>
</tbody>
</table>
<tbody>
<tr>
<td style="vertical-align: top; 70%">
<object id="pl" style=" 100%; height: 519px" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6"
viewastext="">
<param value="" name="URL" />
<param value="1" name="rate" />
<param value="0" name="balance" />
<param value="0" name="currentPosition" />
<param value="" name="defaultFrame" />
<param value="1" name="playCount" />
<param value="-1" name="autoStart" />
<param value="0" name="currentMarker" />
<param value="-1" name="invokeURLs" />
<param value="" name="baseURL" />
<param value="50" name="volume" />
<param value="0" name="mute" />
<param value="full" name="uiMode" />
<param value="0" name="stretchToFit" />
<param value="0" name="windowlessVideo" />
<param value="-1" name="enabled" />
<param value="-1" name="enableContextMenu" />
<param value="0" name="fullScreen" />
<param value="" name="SAMIStyle" />
<param value="" name="SAMILang" />
<param value="" name="SAMIFilename" />
<param value="" name="captioningID" />
<param value="0" name="enableErrorDialogs" />
<param value="6482" name="_cx" />
<param value="6350" name="_cy" />
</object>
</td>
<td style="vertical-align: top; 30%">
播放列表<br />
<script language="javascript" type="text/javascript">
// JScript 文件
function loadXML(xmlFile)
{
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load(xmlFile);
}
else
{
return null;
}
return xmlDoc;
}
//一个检查xml文件的函数
checkXMLDocObj = function(xmlFile)
{
var xmlDoc = loadXML(xmlFile);
if(xmlDoc==null)
{
alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!');
window.location.href='/Index.aspx';
}
return xmlDoc;
}
function doo()
{
// 定义一个读取xml文件的对象
var xmlDoc = checkXMLDocObj('http://s-sps:801/huojian.xml');
var name = new Array();
var url = new Array();
var sum=xmlDoc.getElementsByTagName("pp").length;
for(var i=0; i<sum; i++)
{
url[i]=xmlDoc.getElementsByTagName('media/pp')[i].getAttribute('url');
name[i]=xmlDoc.getElementsByTagName('media/pp')[i].getAttribute('name');
}
var str = new Array();
for(var i=0; i<sum; i++)
{
str[i]="return play('"+url[i]+"')";
}
document.write('<table border="0" cellpadding="0" cellspacing="0">');
for(var i=0; i<sum; i++)
{
var x = i+1;
document.write('<tr><td style="background-image: url(playbj.GIF);background-position:center;background-repeat:no-repeat; 20px;height:24px; text-align:center;vertical-align:middle;" >'+x+'</td><td>');
document.write('<input id=Button1 type=button value='+name[i].toString()+' onclick="'+str[i]+'" style="padding:2 0 0 2; border:0px;vertical-align:middle; background-color:#fff;text-align:left;">');
document.write('</td></tr>');
document.write('<tr><td style="height:1px;"><td><tr>');
}
document.write('</table>');
}
function play(urlx)
{
document.all.item("pl").url=urlx;
document.all.item("pl").controls.play();
}
doo();
</script>
</td>
</tr>
<tr>
<td id="_invisibleIfEmpty" style="display: none" valign="top" width="100%" colspan="2"
name="_invisibleIfEmpty">
</td>
</tr>
<script language="javascript"> if(typeof(MSOLayout_MakeInvisibleIfEmpty) == "function") {MSOLayout_MakeInvisibleIfEmpty();}</script>
</tbody>
</table>