ASP.NET播客(留言时间,投票IP,留言限字数,上传视频)

留言发布时间功能:
界面:
前台代码:
在Datalist控件中:
<%#getIsDate(Convert.ToString(Eval("issuanceDate")))%>发布的评论:</td>
后台代码:  
  //返回发布的时间
    public string getIsDate(string date)
    {
        //转换时间
        DateTime isDate = Convert.ToDateTime(date);
        //获取当前时间
        DateTime nowDate = DateTime.Now;
        //获取两个时间的差
        TimeSpan ts = nowDate - isDate;
        //将时间差转换为分
        int second = Convert.ToInt32(ts.TotalSeconds) / 60;
 
        if (second == 0)
        {
            return "60秒内";
        }
        else
            if (second < 60)
            {
                return second.ToString() + "分钟前";
            }
            else if (second > 60 && second < 1440)
            {
                return Convert.ToString(second / 60) + "小时前";
            }
            else
                return date;
    }
 
投票获取IP,防止再投:
    //将视频id和用户ip保存到数据库中防止重复投票
    protected void addPoll(string sqlUpd, string videoId)
    {
        //获取当前用户的ip
        string userIP = Request.UserHostAddress.ToString();
        //编写SQL查询用户ip是否已对该视频投票
        string sqlSel = "select * from videoPoll where ip='" + userIP + "' and videoId='" + videoId + "'";
        if (!(operateData.getCount(sqlSel) > 0))
        {
            //如果未投票执行SQL语句进行投票
            operateData.execSql(sqlUpd);
            //编写SQL语句记录用户的ip和视频id
            string sqlIns = "insert videoPoll values('" + userIP + "'," + videoId + ")";
            operateData.execSql(sqlIns);
            RegisterStartupScript("""<script>alert('成功!')</script>");
        }
        else
        {
            RegisterStartupScript("""<script>alert('一个视频只可以投一次')</script>");
        }
    }
 
用JS限制留言字数:
 <script language="javascript"> 
        function change()
        {
        //获取评论文本框中的值
            var str=document.getElementById('txtContent').value;
            //获取当前还可以输入的字符数量
            var sum=500-str.length;
            //判读是否还可以输入字符
            if(sum<=0)
            {
            //设置Label控件显示文本为红色
              document.getElementById('labCount').style.color="Red";
              //截取文本框中的字符串,从0位置开始截取,截取到500位
             document.getElementById('txtContent').value=document.getElementById('txtContent').value.substring(0,500);
             //显示可以输入的字符数量
            document.getElementById('labCount').innerHTML=sum;  
         
            }else
            {
            //显示可以输入的字符数量
                document.getElementById('labCount').innerHTML=sum;     
                //设置Label控件的文本颜色
            document.getElementById('labCount').style.color="#006FC3";    
            }           
        }  
    </script>
您还可以输入<asp:Label ID="labCount" runat="server" Text="500"></asp:Label>字符
上传视频:
界面:
前台代码:
<table width="500" border="0" cellspacing="0" cellpadding="0" style="border-right#71add1 1px solidborder-top#71add1 1px solidborder-left#71add1 1px solidborder-bottom#71add1 1px solid;">
      <tr>
        <td height="36" align="left" valign="middle" background="../images/glsc1_05.jpg" class="b" style="width553px">   <span class="hongcu">视频上传</span></td>
      </tr>
      <tr>
        <td  valign="top" style="width553pxheight288px;">
    <table class="huise1" border="0" cellpadding="0" cellspacing="0" style="width490pxheight218px">
        <tr>
            <td colspan="2">
                </td>
            <td colspan="1" style="width154px">
            </td>
        </tr>
        <tr>
            <td style="width127pxtext-aligncenter">
                视频标题:</td>
            <td style="width26px">
                <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox></td>
            <td style="width154pxtext-alignleft;">
                <span style="color#ff0000">*</span></td>
        </tr>
        <tr>
            <td style="width127pxtext-aligncenter">
                视频内容:</td>
            <td style="width26px">
                <asp:TextBox ID="txtContent" runat="server" Height="104px" TextMode="MultiLine" Width="207px"></asp:TextBox></td>
            <td style="width154pxtext-alignleft;" valign="top">
            </td>
        </tr>
        <tr>
            <td style="width127px">
                上传视频:</td>
            <td style="width26pxtext-alignleft">
                <asp:FileUpload ID="fileupVideo" runat="server" /></td>
            <td style="width154pxtext-alignleft">
                <span style="color#ff0033">
                主能上传.flv .avi.wmv</span></td>
        </tr>
        <tr>
            <td style="width127pxheight29px">
                视频类型:</td>
            <td style="width26pxheight29pxtext-alignleft">
                <asp:RadioButtonList ID="radBtnListType" runat="server" Height="21px" RepeatDirection="Horizontal"
                    Width="228px">
                    <asp:ListItem Selected="True">搞笑</asp:ListItem>
                    <asp:ListItem>体育</asp:ListItem>
                    <asp:ListItem>电影</asp:ListItem>
                    <asp:ListItem>动漫</asp:ListItem>
                </asp:RadioButtonList></td>
            <td style="width154pxheight29pxtext-alignleft">
            </td>
        </tr>
        <tr>
            <td colspan="2">
                </td>
            <td colspan="1" style="width154px">
            </td>
        </tr>
        <tr>
            <td colspan="2" style="height24px">
                <asp:Button ID="btnUpVideo" runat="server" OnClick="btnUpVideo_Click" Text="上  传" />
                <asp:Button ID="btnReturn" runat="server" Text="返 回" PostBackUrl="~/user/userIndex.aspx" />
            </td>
            <td colspan="1" style="width154pxheight24px;">
            </td>
        </tr>
    </table>    
    </td> 
      </tr>
      <tr>
        <td align="center" style="width553pxheight22px;" ></td>
      </tr>
      <tr>
        <td height="4" style="width553px"></td>
      </tr>            
    </table>
后台代码:
    //上传转换视频,并将相应的信息存到数据库中
    protected void btnUpVideo_Click(object sender, EventArgs 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);
                    }
                    else
                    {
                        //调用公共类中的changeVideoType方法转换视频格式
                        if (operateMethod.changeVideoType(upFileName, Server.MapPath("../") + playFile, Server.MapPath("../") + imgFile))
                        {
                            //调用自定义insertVideoInfo方法将视频信息保存到数据库中
                            insertVideoInfo(playFile,imgFile);
                            //删除上传的视频
                            File.Delete(upFilePath);
                        }
                        else
                        {
                            RegisterStartupScript("false""<script>alert('上传失败!')</script>");
                            //删除上传的视频
                            File.Delete(upFilePath);
 
                        }
 
                    }
 
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message.ToString());
                }
            }
            else {
                RegisterStartupScript("false""<script>alert('文件格式错误!')</script>");
            }
 
        }
      
    }
 
    //将视频信息存储到数据库中
    private void insertVideoInfo(string playFileh, string imgFile)
    {
        //获取用户名
        string userName =Session["userName"].ToString();
        //获取视频名称
        string videoTitle = txtTitle.Text;
        //获取视频内容
        string videoContent = txtContent.Text;
        //获取当前时间
        string date = DateTime.Now.ToString();
        //获取视频路径
        string videoPath = playFileh;
        //获取图片路径
        string videoPicture = imgFile;
        //获取视频的类型
        string videoType = "";            
        int count = radBtnListType.Items.Count;
        for (int i = 0; i < count; i++)
        {
            if (radBtnListType.Items[i].Selected)
            {
                videoType = radBtnListType.Items[i].Value;
                break;
            }
        }       
        //编写SQL语句将视频的详细信息添加到数据库中
        string sqlInsert = "insert into videoInfo values('" + userName + "','" + videoTitle + "','" + videoContent + "','" + date + "','" + videoPath + "','" + videoPicture + "','" + videoType + "','','','','','')";
        if (operateData.execSql(sqlInsert))
        {
            RegisterStartupScript("true""<script>alert('上传成功!')</script>");
            //编写SQL语句将当前用户的积分增加
            string sqlUpd=" update userInfo set sumMark=sumMark+100 where userName='"+userName+"'" ;
            operateData.execSql(sqlUpd);
 
        }else RegisterStartupScript("true""<script>alert('上传失败!')</script>");
 
    }
    //自定义方法判断视频格式是否正确
    private bool checkExtension(string extension)
    {   
        string[] videoExtension = new string[] { "flv""avi""wmv", };
        bool strReturn =false;
        //遍历数组判断当前上传的视频格式是否正确
        foreach (string var in videoExtension)
        {
            if (var == extension)
            {
                strReturn = truebreak;
            }
        }
        return strReturn;
 
    }
}
类中相关方法:
    /// <summary>
    /// 对视频进行图片截取
    /// </summary>
    /// <param name="fileName">需要截取图片的视频路径</param>
    /// <param name="imgFile">截取图片后保存的图片路径</param>
    
    public static void catchImg(string fileName,string imgFile)
    {
        //获取截图工具路径
        string ffmpeg = System.Web.HttpContext.Current.Server.MapPath("../") + ffmpegtool; 
        //获取截图后保存的路径
        string flv_img = imgFile; 
        //获取截取图片的大小
        string FlvImgSize = sizeOfImg;
        Process pss = new Process();     
        //设置启动程序的路径
        pss.StartInfo.FileName = ffmpeg;       
        pss.StartInfo.Arguments = "   -i   " + fileName + "  -y  -f  image2   -ss 2 -vframes 1  -s   " + FlvImgSize + "   " + flv_img;
        //启动进程
        pss.Start();        
     
    }  
 
    //获取转换工具路径
    public static string ffmpegtool = ConfigurationManager.AppSettings["ffmpeg"];   
    //获取视频的文件夹名
    public static string upFile = "upfile/"
    //获取图片文件的文件夹名
    public static string imgFile = "imgFile/"
    //获取转换后文件的文件夹名
    public static string playFile = "playFile/"
    //文件图片大小
    public static string sizeOfImg = ConfigurationManager.AppSettings["imgSize"];
    //文件大小
    public static string widthOfFile = ConfigurationManager.AppSettings["widthSize"];
    public static string heightOfFile = ConfigurationManager.AppSettings["heightSize"];
 
 
    /// <summary>
    /// 将视频文件转换成flv格式,并保存到playFile文件夹下
    /// </summary>
    /// <param name="fileName">需要转换视频的路径</param>
    /// <param name="playFile">视频转换flv格式后保存的路径</param>
    /// <param name="imgFile">在视频文件中抓取图片后保存路径</param>
    /// <returns>成功:返回图片虚拟地址;   失败:返回空字符串</returns>
    public static  bool  changeVideoType(string fileName, string playFile, string imgFile)
    {
        //获取视频转换工具的路径
        string ffmpeg= System.Web.HttpContext.Current.Server.MapPath("../") + ffmpegtool;
        //获取需要转换的视频路径
        string Name = System.Web.HttpContext.Current.Server.MapPath("../") + upFile + "/" + fileName;
        if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(Name)))
        {
            return false;
        }              
        //获取视频转换后需要保存的路径
        string flv_file = playFile;
        //创建Process对象
        Process pss = new Process();
        //不显示窗口
        pss.StartInfo.CreateNoWindow = false;
        //设置启动程序的路径
        pss.StartInfo.FileName = ffmpeg;
        //设置执行的参数
        pss.StartInfo.Arguments = " -i " + Name + " -ab 128 -ar 22050 -qscale 6 -r 29.97 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
       
        try
        {
            //启动转换工具          
            pss.Start();
            while (!pss.HasExited)
            {
                continue;               
            }
           
            //截取视频的图片
            catchImg(Name, imgFile);
            System.Threading.Thread.Sleep(4000);
            if (!File.Exists(imgFile))
            {
                File.Copy(System.Web.HttpContext.Current.Server.MapPath("../") + "imgHead\default.gif", imgFile);
            }
 
            return true;
        }
        catch
        {
            return false;
        }       
       
    }
原文地址:https://www.cnblogs.com/zt102545/p/3321236.html