Asp.net简单的视频上传与XML文件增删!(及视频在线转换)
Asp.net!
Video.aspx
---------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Video.aspx.cs" Inherits="Admin_Video_Video" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!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>
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<!--再用js控制显示-->
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="98%">
<tr>
<td colspan="2" style="height: 21px">
<span style="font-weight: bold; color: #0099cc">添加视频:</span></td>
</tr>
<tr>
<td align="right" width="10%">
视频文件:
</td>
<td width="90%">
<input type="file" id="fileUpload" runat="server" style="width: 273px"/></td>
</tr>
<tr>
<td align="right" width="10%">
视频名称:
</td>
<td width="90%">
<asp:TextBox ID="txtViName" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtViName"
Display="Dynamic" ErrorMessage="不能为空!" ValidationGroup="submite"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td colspan="2" style="height: 24px">
<asp:Button ID="btnOK" runat="server" Text="添加" OnClick="btnOK_Click" />
<asp:Button ID="btnReset" runat="server" Text="返回" OnClick="btnReset_Click" />
<asp:Label ID="Label2" runat="server" Text="Label" ForeColor="Red"></asp:Label></td>
</tr>
</table>
<hr color="#0099cc" size="1" width="99%" />
<table align="center" border="0" cellpadding="0" cellspacing="0" width="98%">
<tr>
<td colspan="2" style="height: 21px">
<span style="font-weight: bold; color: #0099cc">视频列表</span>
</td>
</tr>
<tr>
<td colspan="2" width="100%">
<asp:GridView ID="gvMyList" runat="server" AutoGenerateColumns="False" CellPadding="4"
CellSpacing="1" EmptyDataText="没有数据!" ForeColor="#333333" GridLines="None" HeaderStyle-BackColor="#0099cc"
HorizontalAlign="Center" Width="98%" OnRowDeleting="gvMyList_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="全选/反选">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="check" runat="server" />
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Flv_Id") %>' Visible="false"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Width="10%" />
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="Flv_Id" DataNavigateUrlFormatString=""
DataTextField="Flv_Name" HeaderText="视频名称">
<ItemStyle HorizontalAlign="Center" Width="50%" />
<HeaderStyle HorizontalAlign="Center" Width="50%" />
</asp:HyperLinkField>
<asp:TemplateField HeaderText="基本操作" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("确定要删除吗?")' Text="删除"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Width="10%" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#0099CC" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="height: 24px" width="30%">
<asp:CheckBox ID="checkAll" runat="server" AutoPostBack="True"
Text="全选/反选" OnCheckedChanged="checkAll_CheckedChanged"/>
<asp:LinkButton ID="lbtDelAll" runat="server" CausesValidation="false"
OnClientClick='return confirm("确定删除所选数据吗?")' Text="批量删除" OnClick="lbtDelAll_Click"></asp:LinkButton></td>
<td align="center" style="height: 24px" width="70%">
<webdiyer:aspnetpager id="anpPage" runat="server" alwaysshow="True" centercurrentpagebutton="True"
custominfohtml="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条" custominfotextalign="Center"
firstpagetext="首页" font-size="12px" font-strikeout="False" font-underline="False"
forecolor="Black" horizontalalign="Left" lastpagetext="尾页" nextpagetext="下一页"
pageindexboxtype="DropDownList" pagesize="10"
prevpagetext="上一页" showcustominfosection="Left" showpageindexbox="Always" submitbuttontext="Go"
textafterpageindexbox="页" textbeforepageindexbox="转到" urlpaging="True" width="99%"
wrap="False" OnPageChanged="anpPage_PageChanged"></webdiyer:aspnetpager>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
-----------------------------
Video.aspx.cs
主要代码!!!!(增加了删除数据库中Flv文件信息外增加删除对应的FLV文件)
-----------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Xml;
public partial class Admin_Video_VideoAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
#region 绑定字段
private void BindData()
{
DataSet ds = BLL.FlvBLL.GetList();
anpPage.RecordCount = ds.Tables[0].Rows.Count;
gvMyList.DataSource = BLL.FlvBLL.GetList(ds, anpPage.PageSize, anpPage.CurrentPageIndex - 1);
gvMyList.DataKeyNames = new string[] { "Flv_Id" };
gvMyList.DataBind();
}
#endregion
#region 上传视频
protected void btnOK_Click(object sender, EventArgs e)
{
if (this.txtViName.Text == "")
{
this.Label2.Text = "文件名不能为空";
}
else
{
string FName = this.txtViName.Text;
if (fileUpload.Value.Length > 0)
{
string path = this.fileUpload.PostedFile.FileName;//文件路径
int a = path.LastIndexOf("\\"); // //在路径里是第几位
int b = path.LastIndexOf("."); // 获取.后面文件名的后缀名
string upload = path.Substring(b + 1);
if (upload == "exe")
{
//Response.Write("<script>alert('不用上传后缀名为.exe的文件')</script>");
this.Label2.Text = "不用上传后缀名为.exe的文";
}
else if (upload == "com")
{
//Response.Write("<script>alert('不用上传后缀名为.com的文件')</script>");
this.Label2.Text = "不用上传后缀名为.com的文";
}
else if (upload == "COM")
{
//Response.Write("<script>alert('不用上传后缀名为.com的文件')</script>");
this.Label2.Text = "不用上传后缀名为.exe的文";
}
else if (upload == "EXE")
{
//Response.Write("<script>alert('不用上传后缀名为.exe的文件')</script>");
this.Label2.Text = "不用上传后缀名为.com的文";
}
else if (upload == "flv")
{
string upFileName = path.Substring(a + 1);//文件名
string extension = System.IO.Path.GetExtension(upFileName);
string fileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
string upFilePath = Server.MapPath(@"~\Flv\"); //文件上传存的地方
string UpFName = upFilePath + fileName;
this.fileUpload.PostedFile.SaveAs(UpFName);
XmlDocument doc = new XmlDocument();//实例化一个XML文档doc
doc.Load(Server.MapPath(@"~\vcastr.xml"));
XmlNode root = doc.SelectSingleNode("vcaster");//实例化一个根节点
XmlElement ele = doc.CreateElement("item");//创建一个XML元素
ele.InnerText = FName;
ele.SetAttribute("item_url", "Flv/" + fileName);
ele.SetAttribute("item_title", FName);
root.AppendChild(ele);//根节点添加孩子节点
doc.Save(Server.MapPath(@"~\vcastr.xml"));//保存
//Page.ClientScript.RegisterStartupScript(this.GetType(), "alt", "<script>alert('上传成功!');</script>");
Model.Flv flv = new Model.Flv();
flv.Flv_Name = FName;
flv.Flv_Path = "Flv/" + fileName;
if (!BLL.FlvBLL.GetReaderNa(FName).Read())
{
//Page.ClientScript.RegisterStartupScript(this.GetType(), "alt", "<script>alert('该管理员名称已存在!');</script>");
txtViName.Text = "";
this.Label2.Text = "该视频已存在";
}
else
{
int i = BLL.FlvBLL.Insert(flv);
if (i > 0)
{
this.Label2.Text = "添加成功";
}
else
{
this.Label2.Text = "添加失败";
}
//Response.Write("<script>alert('上传成功')</script>");
this.Label2.Text = "上传成功";
BindData();
}
}
else
{
//Response.Write("<script>alert('请上传FLV的文件!')</script>");
this.Label2.Text = "请上传FLV的文件";
}
}
else
{
//Response.Write("<script>alert('单击浏览选择上传文件')</script>");
this.Label2.Text = "单击浏览选择上传文件";
}
}
}
#endregion
#region 重置按钮
protected void btnReset_Click(object sender, EventArgs e)
{
//txtViName.Text = "";
}
#endregion
#region 删除所有
protected void lbtDelAll_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in gvMyList.Rows)
{
CheckBox check = (CheckBox)gvRow.FindControl("check");
if (check.Checked == true)
{
Label label = (Label)gvRow.FindControl("lblId");
int delId = int.Parse(label.Text); ;
BLL.FlvBLL.Delete(delId);
Model.Flv Flv = BLL.FlvBLL.GetModel(delId);
deleteFlv(Flv.Flv_Name);
}
}
BindData();
}
#endregion
#region 全选
protected void checkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in gvMyList.Rows)
{
CheckBox check = (CheckBox)gvRow.FindControl("check");
if (checkAll.Checked == true)
{
check.Checked = true;
}
else
{
check.Checked = false;
}
}
}
#endregion
#region 删除单行
protected void gvMyList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int delId = int.Parse(gvMyList.DataKeys[e.RowIndex].Value.ToString());
Model.Flv Flv = BLL.FlvBLL.GetModel(delId);
string flvname = Flv.Flv_Name;
deleteFlv(flvname);
string flvpath = Flv.Flv_Path;
File.Delete(Server.MapPath(@"~\" + flvpath));
BLL.FlvBLL.Delete(delId);
BindData();
}
#endregion
#region 分页绑定
protected void anpPage_PageChanged(object sender, EventArgs e)
{
BindData();
}
#endregion
#region 删除视频对应XML列表
public void deleteFlv(string Flv_Name)
{
XmlDocument doc = new XmlDocument();//实例化一个XML文档doc
doc.Load(Server.MapPath(@"~\vcastr.xml"));
XmlNodeList Node_List = doc.SelectSingleNode("vcaster").ChildNodes;
foreach (XmlNode Node in Node_List)
{
if (Node.InnerText == Flv_Name)
{
Node.ParentNode.RemoveChild(Node);
//注意这边如果使用XmlElement类中的RemoveAll();方法XML会留下空节点:<user></user>
}
}
doc.Save(Server.MapPath(@"~\vcastr.xml"));//保存
}
#endregion
}
-----------------------------
vcastr.xml
-----------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8"?>
<vcaster>
</vcaster>
-----------------------------
一般大家都可以看明白!
里面包含的方法也相当简单。高手路过即可。
分享一个 未完成的视频转换 源码 但有一个Bug 可能是 工具原因 与网上的 大多数的视频在线转换大同小异 只不过在某些参数与 工具上有些变化。。需要的 邮箱:lz21@vip.qq.com 一起研究
这个家伙很懒