コード例 #1
0
ファイル: ConvertVideo.cs プロジェクト: dmhai/ColoPay
 /// <summary>
 /// 上传视频
 /// </summary>
 /// <param name="postFile"></param>
 /// <param name="isConvert">是否进行格式转换</param>
 /// <param name="savePath">视频保存的路径</param>
 /// <param name="configSize">上传视频限制的大小</param>
 /// <param name="isGetImg">是否获取视频缩略图</param>
 /// <param name="isGetSpan">是否获取视频的时间</param>
 /// <param name="model">返回处理结果</param>
 /// <param name="extend">转换为哪种格式的视频 eg:".flv"</param>
 /// <returns></returns>
 public bool UploadVideo(HttpPostedFile postFile, bool isConvert, string savePath, int?configSize, bool isGetImg, bool isGetSpan, out VideoModel model, string extend)
 {
     model = new VideoModel();
     if (postFile != null)
     {
         if (configSize.HasValue)
         {
             if (postFile.ContentLength > configSize.Value)
             {
                 _errorMessage = "上传文件过大";
                 return(false);
             }
         }
         #warning   目录待统一
         string ext = Path.GetExtension(postFile.FileName);
         ext = String.IsNullOrWhiteSpace(ext) ? "" : ext.ToLower();
         if (!AllowFileExt.Contains(ext) || String.IsNullOrWhiteSpace(ext))
         {
             _errorMessage = "上传文件格式不正确!";
             return(false);
         }
         string fileName = Guid.NewGuid().ToString("N", System.Globalization.CultureInfo.InvariantCulture) + ext;
         string pathStr  = HttpContext.Current.Server.MapPath("/" + savePath);
         if (!System.IO.Directory.Exists(pathStr))
         {
             System.IO.Directory.CreateDirectory(pathStr);
         }
         string path = savePath + fileName;
         postFile.SaveAs(HttpContext.Current.Server.MapPath(path));
         model.SavePath = path;
         if (isConvert)
         {
             string convertPath  = Path.ChangeExtension(path, extend);
             string destFileName = Path.Combine(pathStr, Path.ChangeExtension(postFile.FileName, extend));
             ConvertFlv(HttpContext.Current.Server.MapPath(path), destFileName);
             model.SavePath = convertPath;
         }
         if (isGetImg)
         {
             string imgpath     = Path.ChangeExtension(path, ".jpg");
             string jpgFileName = Path.Combine(HttpContext.Current.Server.MapPath(path), Path.ChangeExtension(path, ".jpg"));
             CreateThumb(HttpContext.Current.Server.MapPath(path), HttpContext.Current.Server.MapPath(jpgFileName));
             model.ImgPath = imgpath;
         }
         if (isGetSpan)
         {
             TimeSpan totalTime = GetVideoTotalTime(HttpContext.Current.Server.MapPath(path));
             int      ts        = TimeParser.TimeToSecond(totalTime.Hours, totalTime.Minutes, totalTime.Seconds);
             model.VideoSpan = ts;
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }