input type="file" 上传文件,若是flv格式,则截取保存一张图片asp.net

来源:互联网 发布:java socket通信 编辑:程序博客网 时间:2024/05/04 07:41

前台:

  <input width="200px" class="TextClass" id="AttachInfo" runat="server" type="file" />        &nbsp;<asp:Label ID="lbl_AttachInfo" runat="server"></asp:Label>          <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" />

后台:

  protected void btnUpload_Click(object sender, EventArgs e)        {            string RowGuid = Guid.NewGuid().ToString();            //获取文件名            string FilePath = AttachInfo.Value;            //文件后缀名            string suffixName = FilePath.Substring(FilePath.LastIndexOf('.'));            //附件大小            int FileLen = AttachInfo.PostedFile.ContentLength;            lbl_AttachInfo.Text = FileLen.ToString();            //将flv格式的文件重新保存一份图片             if (suffixName.ToUpper() == ".FLV")            {                string strAttURL = System.Web.HttpContext.Current.Server.MapPath("~/TempFile/") + RowGuid + "\\";//服务端保存的路径                if (Directory.Exists(strAttURL) == false)//如果不存在就创建file文件夹                {                    Directory.CreateDirectory(strAttURL);                }                AttachInfo.PostedFile.SaveAs(strAttURL + AttachInfo.PostedFile.FileName);//将文件保存在服务器的路径上                CatchImg(AttachInfo.PostedFile.FileName, RowGuid, System.Web.HttpContext.Current.Server.MapPath("~/TempFile/"));            }        }        #region 从视频中截取一张图片保存        public string CatchImg(string fileName, string RowGuid, string strAttPath)        {            string sizeOfImg = "240x180";            string ffmpeg = HttpContext.Current.Server.MapPath("~/ffmpeg/ffmpeg.exe");            string flv_img = fileName.ToLower().Replace(".flv", ".jpg");            string TostrFullFilePath = strAttPath + RowGuid + "\\" + flv_img;            string strFullFilePath = strAttPath + RowGuid + "\\" + fileName;//客户端路径            string strPartUrl = System.Web.HttpContext.Current.Request.ApplicationPath + "/" + "TempFile/" + RowGuid + "/" + flv_img;            if (!File.Exists(TostrFullFilePath))            {                string FlvImgSize = sizeOfImg;                //                System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);                ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;                //                ImgstartInfo.Arguments = "   -i   " + strFullFilePath + "  -y  -f  image2   -ss 2 -vframes 1  -s   " + FlvImgSize + "   " + TostrFullFilePath;                try                {                    System.Diagnostics.Process.Start(ImgstartInfo);                }                catch                {                    return "";                }                //flv_img                if (System.IO.File.Exists(TostrFullFilePath))                {                    return strPartUrl;                }                return "";            }            else            {                return strPartUrl;            }        }        #endregion    }

注文件ffmpeg.exe 在自己的百度网盘下载,文件夹名为ffmpeg

将文件保存为流,入库

  public static byte[] RetriveUploadFileByte(HtmlInputFile iFile)        {            if (iFile.PostedFile.ContentLength > 0)            {                byte[] buffer = new byte[iFile.PostedFile.ContentLength];                iFile.PostedFile.InputStream.Read(buffer, 0, iFile.PostedFile.ContentLength);                return buffer;            }            return new byte[0];        }
0 0