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.IO;
public partial class Admin_HLSP_TJ : System.Web.UI.Page
{
string[] videoExtension = new string[] { "flv", "avi", "wmv", };
HLSP_table h = new HLSP_table();
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["user"] == null)
{
Response.Redirect("Login.aspx");
}
//判断用户是否登录
//if (Session["userName"] == null)
//{
// //未登录给出提示并返回到首页
// Response.Write("<script>alert('请您先登录!');location='../index.aspx'</script>");
//}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string upFileName = fileupVideo.FileName;
//判断是否选择了文件
if (this.fileupVideo.HasFile)
{
//获取文件的扩展名
string upExtension = upFileName.Substring(upFileName.LastIndexOf(".") + 1);
//判断扩展名是否正确
if (checkExtension(upExtension))
{
//获取上传文件所保存的路径
string upFilePath = Server.MapPath("../upFile/") + upFileName;
//将文件保存到指定路径中
fileupVideo.SaveAs(upFilePath);
//获取当前时间
string saveName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
//获取视频转换后所保存的路径及文件名
string playFile = "playFile/" + saveName + ".flv";
//获取图片所保存的路径及名称
string imgFile = "imgFile/" + saveName + ".jpg";
try
{
//判断上传的文件是否为flv格式
if (upExtension == "flv")
{
//如果为flv格式直接保存到指定路径下
File.Copy(fileupVideo.PostedFile.FileName, Server.MapPath("../" + playFile));
//调用公共类中的catchImg方法截取视频图片
operateMethod.catchImg(Server.MapPath("../" + playFile), Server.MapPath("../" + imgFile));
//调用自定义insertVideoInfo方法将视频的信息保存到数据库中
insertVideoInfo(playFile, imgFile);
if (System.IO.File.Exists(Server.MapPath("~/upFile/" + upFileName)))
{
System.IO.File.Delete(Server.MapPath("~/upFile/" + upFileName));
}
fileupVideo.SaveAs(HttpContext.Current.Server.MapPath(imgFile));
}
else
{
//调用公共类中的changeVideoType方法转换视频格式
if (operateMethod.changeVideoType(upFileName, Server.MapPath("../") + playFile, Server.MapPath("../") + imgFile))
{
//调用自定义insertVideoInfo方法将视频信息保存到数据库中
insertVideoInfo(playFile, imgFile);
//删除上传的视频
if (System.IO.File.Exists(Server.MapPath("~/upFile/" + upFileName)))
{
System.IO.File.Delete(Server.MapPath("~/upFile/" + upFileName));
}
}
else
{
RegisterStartupScript("false", "<script>alert('上传失败!')</script>");
//删除上传的视频
if (System.IO.File.Exists(Server.MapPath("~/upFile/" + upFileName)))
{
System.IO.File.Delete(Server.MapPath("~/upFile/" + upFileName));
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
else
{
RegisterStartupScript("false", "<script>alert('文件格式错误!')</script>");
}
}
}
private bool checkExtension(string extension)
{
bool strReturn = false;
//遍历数组判断当前上传的视频格式是否正确
foreach (string var in this.videoExtension)
{
if (var == extension)
{
strReturn = true; break;
}
}
return strReturn;
}
protected void insertVideoInfo(string playFile, string imgFile)
{
string name = TextBox1.Text;
string shijian = DateTime.Now.ToString();
string neirong = TextBox2.Text;
int j = h.Add(name, neirong, playFile, imgFile, shijian);
if (j > 0)
{
Response.Write("<script language=javascript>alert('视频上传成功')</script>");
Response.Write("<script language=javascript>document.location='HLSP_tianjia.aspx'</script>");
}
}
}