添加视频,经常报错,老是这个情况,怎么解决,IIS7,内存4G

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Text;using YDSystem.ZXF.Entity;
    using YDSystem.ZXF.BLL;
    using YDSystem.ZXF.Common;
    using YDSystem.ZXF.Config;
    using System.Data;
    using System.Diagnostics;public partial class Manage_Video_AddVideo : YDSystem.ZXF.Page.AdminBase
    {
        private string action;  //操作类型
        private int classId;   //分类的id
        protected int videoId;  //当前修改的视频id
        protected string titlePicUrl;   //标题图片url
        public string titlePicUrl2;   //标题图片url
        protected string scrollPicUrl;  //滚动图片url
        protected int sourceType = 1;  //文件类型 0-本地上传 1-从服务器选择或链接
        protected bool isScroll = false;    //是否为首页滚动
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取URL参数
            action = Utils.GetRequest("action");
            classId = Utils.GetIntRequest("cid");
            videoId = Utils.GetIntRequest("vid");
            //首次加载
            if (!this.IsPostBack)
            {
                this.InitPage();
            }
            else
            {
                //设定下拉框中频道分类的css
                this.SetChannelCss();
            }
        }    //绑定下拉列表
        protected void BindDropdown()
        {
            IList<int> indexs = VideoBinder.BindDropdownClass(ddlColumns);
            //保存到ViewState中
            ViewState["firstIndexes"] = indexs;        //添加“全部分类”项
            ddlColumns.Items.Insert(0, new ListItem("[--请选择分类--]", "0"));
        }    //设定频道分类的css样式,方便管理员区分不同的分类
        private void SetChannelCss()
        {
            if (ViewState["firstIndexes"] == null)
            {
                return;
            }        IList<int> indexs = (IList<int>)ViewState["firstIndexes"];
            //设定一级分类的样式,方便查看
            foreach (int index in indexs)
            {
                ddlColumns.Items[index + 1].Attributes.CssStyle["background-color"] = "#48A6E6";
                ddlColumns.Items[index + 1].Attributes.CssStyle["font-weight"] = "bold";
            }
        }    //初始化页面
        protected void InitPage()
        {
            if (action == "add")    //添加模式
            {
                lTitle.Text = "添加电影";
                btnSubmit.Text = "添加后继续添加";
            }
            else if (action == "edit")   //修改模式
            {
                lTitle.Text = "修改电影";
                btnSubmit.Text = "修 改";
                btnAddAndBack.Visible = false;
            }        //暂无图片
            titlePicUrl = "/manage/images/nopic_01.gif";
            titlePicUrl2 = "/manage/images/nopic_01.gif";
            scrollPicUrl = "/manage/images/nopic_01.gif";        //类别
            CheckBoxType.DataSource = YDSystem.ZXF.SQLServerDAL.AmusementTypeDAL.GetList("a.MName,b.Id,b.Tname", "");
            CheckBoxType.DataBind();        //年份
            DataTable dt= YDSystem.ZXF.SQLServerDAL.Year.GetList("");
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                ListItem item = new ListItem();
                item.Text = dt.Rows[i]["Yname"].ToString();
                item.Value = dt.Rows[i]["id"].ToString();
                Year.Items.Add(item);
            }        //地区
            dt= YDSystem.ZXF.SQLServerDAL.Area.GetList("a.MName,b.Id,b.Aname", "");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem item = new ListItem();
                item.Text = dt.Rows[i]["Aname"].ToString();
                item.Value = dt.Rows[i]["id"].ToString();
                Area.Items.Add(item);
            }        //类别
            dt= YDSystem.ZXF.SQLServerDAL.Class.GetList("");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem item = new ListItem();
                item.Text = dt.Rows[i]["Cname"].ToString();
                item.Value = dt.Rows[i]["id"].ToString();
                Class.Items.Add(item);
            }
                    //绑定分类列表
            //this.BindDropdown();        if (action == "add")    //添加模式
            {
                //选定视频分类
                if (classId > 0) //添加指定分类的视频
                {
                    ddlColumns.SelectedIndex = ddlColumns.Items.IndexOf(ddlColumns.Items.FindByValue(classId.ToString()));
                }
                else
                {
                    ddlColumns.SelectedIndex = 0;
                }
            }
            else if (action == "edit")   //修改模式
            {
                //从数据库中读取该视频
                Video video = new VideoBLL().GetItemById(videoId);            if (video == null)
                {
                    Utils.ShowAdminError("该视频不存在");
                }            //初始化各控件
                ddlColumns.SelectedIndex = ddlColumns.Items.IndexOf(ddlColumns.Items.FindByValue(video.ClassId.ToString()));            //标题
                txtTitle.Text = video.Title;            //类型
                foreach (RepeaterItem item in CheckBoxType.Items)
                {
                    CheckBox chProTypeEx = (CheckBox)item.FindControl("CheckBox");
                    if (video.TypeIds.IndexOf(chProTypeEx.Text)!=-1)
                    {
                        chProTypeEx.Checked = true;
                    }
                }
                //年份
               foreach(ListItem item in this.Year.Items)
               {
                   if (video.Year.IndexOf(item.Text)!=-1)
                   {
                       item.Selected = true;
                       break;
                   }
               }
               //地区
               foreach (ListItem item in this.Area.Items)
               {
                   if (video.Area.IndexOf(item.Text) != -1)
                   {
                       item.Selected = true;
                       break;
                   }
               }
                //类别
               foreach (ListItem item in this.Class.Items)
               {
                   if (video.Class.IndexOf(item.Text) != -1)
                   {
                       item.Selected = true;
                       break;
                   }
               }           //演员
               Actor_text.Text = video.Actor;
              
                //导演
               this.Text_Director.Text = video.Director;
                
                //简介
                txtIntro.Text = video.Intro;            //文件来源类型
                sourceType = video.SourceType;
                if (sourceType == 0)    //本地上传
                {
                    if (video.VideoPath == string.Empty)
                    {
                        lVideoFile.Text = "暂无视频";
                    }
                    else
                    {
                        lVideoFile.Text = MakeLink(video.VideoPath,"查看该视频");
                    }
                }
                else if (sourceType == 1)   //链接
                {
                    if (video.VideoPath == string.Empty)
                    {
                        lVideoUrl.Text = "暂无视频";
                    }
                    else
                    {
                        lVideoUrl.Text = MakeLink(video.VideoPath,"查看该视频");
                    }
                }            //标题图片
                if (video.TitlePic != string.Empty)
                {
                    titlePicUrl2 = video.TitlePic;
                }
                if (video.TitlePicW != string.Empty)
                {
                    titlePicUrl = video.TitlePicW;
                }            //首页滚动
                if (video.ScrollPic != string.Empty)    //首页滚动
                {
                    isScroll = true;
                    rblScroll.SelectedIndex = 0;                scrollPicUrl = video.ScrollPic;
                }
                else    //非首页滚动
                {
                    isScroll = false;
                    rblScroll.SelectedIndex = 1;
                }            //推荐
                rblRecommanded.SelectedIndex = video.IsRecommanded ? 0 : 1;            //隐藏
                rblHidden.SelectedIndex = video.IsHidden ? 0 : 1;            //标签
                txtTags.Text = Utils.TrimComma(video.Tags);            //关键字
                txtKeywords.Text = Utils.TrimComma(video.Keywords);            //搜索关键字
                video.MetaKeywords = txtMetaKeywords.Text.Trim();
                if (txtMetaKeywords.Text.Trim() == "")
                {
                    video.MetaKeywords = video.Title;
                }            //搜索描述
                video.MetaDescription = txtMetaDescription.Text.Trim();
                if (txtMetaDescription.Text.Trim() == "")
                {
                    video.MetaKeywords = txtIntro.Text.Trim();
                }            //播放次数
                txtHits.Text = video.Hits.ToString();            //下载次数
                txtDownloads.Text = video.Downloads.ToString();
            }        //显示标题图片
            imgTitlePic.ImageUrl = titlePicUrl;
            imgTitlePic2.ImageUrl = titlePicUrl2;
            imgScrollPic.ImageUrl = scrollPicUrl;
        }    //保存数据
        protected void SaveData(bool back)
        {
            if (!this.IsValid)  //未通过验证
            {
                return;
            }        //标题
            string title = txtTitle.Text.Trim();        //所属分类
            int classId=-1 ;        Video video = null; //视频实例
            VideoBLL bll = new VideoBLL();        if (action == "add")
            {
                if (!VideoConfig.IsSameTitleAllowed && bll.Exists(title)) //不允许重名
                {
                    ShowMessage(lblMsg, -450);
                    return;
                }            //创建新实例
                video = new Video();
            }
            else if (action == "edit")
            {
                if (!VideoConfig.IsSameTitleAllowed && bll.ExistsWhere(string.Format("Title='{0}' AND [Id]<>{1}", Utils.EscapeSqlValue(title), videoId))) //不允许重名
                {
                    ShowMessage(lblMsg, -451);
                    return;
                }            //从数据库获取该视频
                video = bll.GetItemById(videoId);
                if (video == null)
                {
                    Utils.ShowAdminError("该视频不存在或已被删除");
                }
            }
      

  2.   


            //分类
            video.ClassId = classId;        //标题
            video.Title = title;        //简介
            video.Intro = txtIntro.Text;        //类型
            video.TypeIds = "";
            foreach (RepeaterItem item in CheckBoxType.Items)
            {
                CheckBox chProTypeEx = (CheckBox)item.FindControl("CheckBox");
                if (chProTypeEx.Checked == true)
                {
                    video.TypeIds += chProTypeEx.Text + ",";
                }
            }
            video.TypeIds = Utils.TrimComma(video.TypeIds);
            //年份
            video.Year = this.Year.SelectedItem.Text;
            //地区
            video.Area =   this.Area.SelectedItem.Text ;
            //类别
            video.Class =   this.Class.SelectedItem.Text ;
            //演员
            video.Actor = this.Actor_text.Text.Trim();
            //导演
            video.Director = this.Text_Director.Text.Trim();
            //类型电影
            video.MediaType = 0;
            //文件来源类型
            int sourceType = Convert.ToInt32(rblSourceType.SelectedValue);
            if (sourceType == 0)    //本地上传
            {
                //保存视频文件
                if (fileVideo.HasFile)
                {
                    //原文件大小
                    int fileSize = fileVideo.PostedFile.ContentLength;                //扩展名 带“.”
                    string extension = Path.GetExtension(fileVideo.FileName).ToLower();                //相对保存路径
                    string filePath = GlobalConfigManage.GetUploadFileFullName(extension);                //保存
                    fileVideo.SaveAs(Server.MapPath(filePath));                //把上传文件的路径和大小信息写入实例中
                    video.VideoPath = filePath;
                    video.VideoSize = fileSize;
                }
                
            }
            else    //链接
            {
                if (txtVideoPath.Text.Trim()!="")
                {
                    video.VideoPath = txtVideoPath.Text.Trim();
                }
            }
            video.SourceType = sourceType;        //推荐
            video.IsRecommanded = Convert.ToBoolean(rblRecommanded.SelectedValue);        //隐藏
            video.IsHidden = Convert.ToBoolean(rblHidden.SelectedValue);        //标签
            video.Tags = Utils.AddComma(txtTags.Text);        //关键字
            video.Keywords = Utils.AddComma(txtKeywords.Text);        //搜索关键字
            video.MetaKeywords = txtMetaKeywords.Text.Trim();
            if (txtMetaKeywords.Text.Trim() == "")
            {
                video.MetaKeywords = title;
            }        //搜索描述
            video.MetaDescription = txtMetaDescription.Text.Trim();
            if (txtMetaDescription.Text.Trim() == "")
            {
                video.MetaDescription = txtIntro.Text.Trim();
            }        //播放次数
            int hits;
            int.TryParse(txtHits.Text.Trim(), out hits);
            
            video.Hits = hits;        //下载次数
            int downloads;
            int.TryParse(txtDownloads.Text.Trim(), out downloads);
            video.Downloads = downloads;        VideoConfig config = VideoConfig.GetConfig();        System.Drawing.Image image = null;
            //缩略图片
            if (fileTitlePic.HasFile)
            {
                //验证图片格式
                if (!Utils.IsImageFormat(Path.GetExtension(fileTitlePic.FileName)))
                {
                    ShowMessage(lblMsg,-902);
                    return;
                }            //创建Image对象
                image = System.Drawing.Image.FromStream(fileTitlePic.FileContent);            //完全文件名
                string fileName = VideoConfig.GetUploadImageFileName();            bool result = YDSystem.ZXF.Common.Configurable.ImageWorker.MakeThumbnail(image, Server.MapPath(fileName), 160, 90);            if (result) //生成缩略图成功
                {
                    //保存之前的标题图片
                    string formerTitlePic = video.TitlePicW;
                    
                    //更新此次的缩略图片到实例中
                    video.TitlePicW = fileName;                //显示图片
                   // titlePicUrl = fileName;                if (action == "edit" && formerTitlePic != string.Empty) //编辑模式下上传了标题图片,需要删除之前的图片
                    {
                        formerTitlePic = Server.MapPath(formerTitlePic);
                        if (File.Exists(formerTitlePic))
                        {
                            try
                            {
                                File.Delete(formerTitlePic);
                            }
                            catch(Exception ex)
                            {
                                ErrorHandler.WriteError(ex);
                            }
                        }
                    }
                }
                else
                {
                    Utils.Alert("图片过小,生成失败 160x90以上");
                    return;
                }
            }
            else
            {
                if (video.TitlePicW=="")
                {
                    Utils.Alert("请选择标题图片");
                    return;
                }
            }
            //
            if (fileTitlePic2.HasFile)
            {
                //验证图片格式
                if (!Utils.IsImageFormat(Path.GetExtension(fileTitlePic2.FileName)))
                {
                    ShowMessage(lblMsg, -902);
                    return;
                }            //创建Image对象
                image = System.Drawing.Image.FromStream(fileTitlePic2.FileContent);            //完全文件名
                string fileName = VideoConfig.GetUploadImageFileName();            bool result = YDSystem.ZXF.Common.Configurable.ImageWorker.MakeThumbnail(image, Server.MapPath(fileName), 250, 280);            if (result) //生成缩略图成功
                {
                    //保存之前的标题图片
                    string formerTitlePic = video.TitlePic;                //更新此次的标题图片到实例中
                    video.TitlePic = fileName;                //显示图片
                    titlePicUrl = fileName;
                    //imgTitlePic.ImageUrl = fileName;                if (action == "edit" && formerTitlePic != string.Empty) //编辑模式下上传了标题图片,需要删除之前的图片
                    {
                        formerTitlePic = Server.MapPath(formerTitlePic);
                        if (File.Exists(formerTitlePic))
                        {
                            try
                            {
                                File.Delete(formerTitlePic);
                            }
                            catch (Exception ex)
                            {
                                ErrorHandler.WriteError(ex);
                            }
                        }
                    }
                }
                else
                {
                    Utils.Alert("图片过小生成失败 250x280以上");
                    return;
                }
            }
            else
            {
                if (video.TitlePic == "")
                {
                   Utils.Alert("请选择标题图片");
                   return;
                }
            }
      

  3.   


            //if (this.fileVideo.HasFile)
            //{
            //    string upFileName = "";
            //    string saveName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            //    upFileName = Server.MapPath("/resource/upload/media/" + saveName+fileVideo.FileName);
            //    this.fileVideo.SaveAs(upFileName);
                
            //    string playFile = Server.MapPath("/resource/upload/media/" + saveName + ".flv");
            //    string imgFile = Server.MapPath("/resource/upload/media/" + saveName + ".jpg");        //    string ffmpeg = Server.MapPath("/resource/upload/media/ffmpeg.exe");
            //    Process p = new Process();//建立外部调用线程   
            //    p.StartInfo.FileName = ffmpeg;//要调用外部程序的绝对路径   
            //    //p.StartInfo.Arguments = " -y -i " + upFileName + " -ab 56k -ar 22050 -b 500k -r 15 -s 240x240 " + playFile; ;//参数(这里就是FFMPEG的参数了)   
            //    p.StartInfo.Arguments = " -y -i " + upFileName + " -y -f image2 -ss 60 -t 0.001 -s 480x480 " + playFile;
            //    p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)   
            //    p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)   
            //    p.StartInfo.CreateNoWindow = false;//不创建进程窗口   
            //    //p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN   
            //    p.Start();//启动线程   
            //    p.BeginErrorReadLine();//开始异步读取   
            //    p.WaitForExit();//阻塞等待进程结束   
            //    p.Close();//关闭进程   
            //    p.Dispose();//释放资源         //    p = new Process();
            //    p.StartInfo.FileName = ffmpeg;//要调用外部程序的绝对路径   
            //    p.StartInfo.Arguments = " -i " + upFileName + " -y -f image2 -ss 60 -t 0.001 -s 120*120 " + imgFile;
            //    p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)   
            //    p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)   
            //    p.StartInfo.CreateNoWindow = false;//不创建进程窗口 
            //    p.Start();//启动线程   
            //    p.BeginErrorReadLine();//开始异步读取   
            //    p.WaitForExit();//阻塞等待进程结束   
            //    p.Close();//关闭进程   
            //    p.Dispose();//释放资源        //    //Session["file"] = fileName;                     //      string PicName =Server.MapPath(Guid.NewGuid().ToString().Replace("-","")+".jpg");        //      System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);        //      startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;        //      startInfo.Arguments = " -i " + Server.MapPath("/resource/upload/media/" + saveName + fileVideo.FileName) + " -y -f image2 -ss " + 10 + " -t 0.001 -s 240*180" + PicName;        //     System.Diagnostics.Process.Start(startInfo);
            //}        //滚动图片
            if (rblScroll.SelectedValue == "1")
            {
                //是滚动图片
                isScroll = true;
            }
            else
            {
                isScroll = false;
            }        bool scrollResult = false;
            string scrollFileName = VideoConfig.GetUploadImageFileName();        if (rblScroll.SelectedValue == "1" && cbxUseTitlePic.Checked == true && image != null)  //首页滚动、与标题图片相同且标题图片不为空
            {
                scrollResult = YDSystem.ZXF.Common.Configurable.ImageWorker.MakeThumbnail(image, Server.MapPath(scrollFileName), 950, 370);
            }
            else if (fileScrollPic.HasFile) //手动选择了滚动图片
            {
                //验证图片格式
                if (!Utils.IsImageFormat(Path.GetExtension(fileScrollPic.FileName)))
                {
                    ShowMessage(lblMsg, -902);                //释放对象
                    if (image != null)
                    {
                        image.Dispose();
                    }
                    return;
                }            image = System.Drawing.Image.FromStream(fileScrollPic.FileContent);            //生成缩略图
                scrollResult = YDSystem.ZXF.Common.Configurable.ImageWorker.MakeThumbnail(image, Server.MapPath(scrollFileName), 950, 370);
            }        if (scrollResult)   //生成滚动图片成功
            {
                string formerScrollPic = video.ScrollPic;            //保存到实例中并显示
                video.ScrollPic = scrollFileName;
                scrollPicUrl = scrollFileName;
                imgScrollPic.ImageUrl = scrollFileName;            if (action == "edit" && formerScrollPic != string.Empty)
                { 
                    //删除之前的滚动图片
                    try
                    {
                        File.Delete(Server.MapPath(formerScrollPic));
                    }
                    catch(Exception ex)
                    {
                        ErrorHandler.WriteError(ex);
                    }
                }
            }        //释放对象
            if (image != null)
            {
                image.Dispose();
            }        int lastResult = 0;
            if (action == "add")
            { 
                //添加者和最后修改者
                video.AddAdmin = video.ModifiedAdmin = MasterBLL.LoggedMasterName;            lastResult = bll.Add(video);            if (lastResult > 0) //添加成功
                {
                    if (back)   //跳转到之前的页面
                    {
                        GoBack("ManageVideo.aspx", lastResult);
                    }                foreach (Control control in form1.Controls)
                    {
                        if (control is TextBox)
                        {
                            ((TextBox)control).Text = "";
                        }
                    }
                }
            }
            else if (action == "edit")
            { 
                //最后修改者
                video.ModifiedAdmin = MasterBLL.LoggedMasterName;            lastResult = bll.Update(video);            if (lastResult > 0)
                {
                    GoBack("ManageVideo.aspx", lastResult);
                }
            }        //显示操作结果
            ShowMessage(lblMsg,lastResult);
        }    //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //保存
            SaveData(false);
        }    //保存后返回
        protected void Button1_Click(object sender, EventArgs e)
        {
            SaveData(true);
        }
      

  4.   

    你也没说程序里做了什么操作呀
    IIS有默认的请求超时、程序池也有内存及CPU的限额设置==
      

  5.   

    应该不是代码问题吧,该关闭的都关了,以前是用的FCK插件添加视频,现在直接用的上传控件
      

  6.   

    如果你硬要用http上传,需要修改配置文件里最大上传限制
    以及Server.ScriptTimeout = 600;
    默认是90秒,设定的时间内没有上传完毕请求将超时,上传失败,这段时间的连接对服务端而言是一种累赘
    如果文件过大,建议用ftp并且断点续传
      

  7.   

    你有监控服务端的负荷吗,比如内存、CPU、出错次数==
    假如有好几个人同时在上传呢?