c# 视频上传

  /// <summary>
        /// 上传视频文件
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadVideoFile()
        {
            var resultMsg = new ReturnResult();
            HttpFileCollectionBase files = Request.Files;
            if (files.Count == 0)
            {
                resultMsg.status = 0;
                resultMsg.msg = "没有上传的文件";
                return Json(resultMsg);
            }
            try
            {
                string root = HostingEnvironment.MapPath("/Uploads/Video/");
                string orginFileName = string.Empty;
                string fileName = string.Empty;
                UserModel currentUser = (UserModel)(System.Web.HttpContext.Current.Session["user"]);
                for (int i = 0; i < files.Count; i++)
                {
                    var file = files[i];
                    if (Path.GetExtension(file.FileName).ToLower() != ".mp4")
                    {
                        resultMsg.status = 0;
                        resultMsg.msg = "不是mp4视频文件";
                        return Json(resultMsg);
                    }
                    string temp_filename = Path.GetFileName(file.FileName);
                    var dir = Path.Combine(root, "temp");
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    string temp_video_file = Path.Combine(dir, temp_filename);                    //保存文件
                    if (System.IO.File.Exists(temp_video_file))
                        System.IO.File.Delete(temp_video_file);
                    file.SaveAs(temp_video_file);
                    orginFileName = file.FileName;
                    fileName = Path.GetFileNameWithoutExtension(temp_video_file);
                    var oss = new OSSFileManager();
                    string bucket = "hucai-simoo";
                    string endpoint = "http://gdoss.xstore.ctyun.cn";
                    string accessKeyId = "6JMTTsEYnXd4h0uMhsIf";
                    string accessKeySecret = "y5VZsEFB2NTI6uzboijbVymXV6JeNZX5IVhLxeWk";
                    string ossPath = "mtqvideo/ar/" + DateTime.Now.ToString("yyyyMMdd") + "/" + Path.GetFileName(temp_video_file);
                    var ossRet = oss.UploadFile(bucket, endpoint, accessKeyId, accessKeySecret, temp_video_file, ossPath);
                    if (ossRet == "0")
                    {
                        double x = 1024;
                        var videoPath = "https://hucai-simoo.gdoss.xstore.ctyun.cn/" + ossPath;
                        //获取文件大小
                        var length = Math.Round(file.ContentLength / x, 2);
                        System.IO.File.Delete(temp_video_file);
                        var videoDal = new VideoDal();
                     
                        var result = videoDal.AddUploadVideoRecord(fileName, length, videoPath, currentUser.UserId);
                        if (result > 0)
                        {
                            resultMsg.msg = "上传完成";
                            resultMsg.response = videoPath;                            
                        }
                        else
                        {
                            resultMsg.status = 0;
                            resultMsg.msg = "视频地址已存在";
                        }
                    }
                    else
                    {
                        resultMsg.status = 0;
                        resultMsg.msg = ossRet;
                    }
                }
            }
            catch (Exception ex)
            {
                resultMsg.status = 0;
                resultMsg.msg = ex.Message;
            }
            return Json(resultMsg);
        }

 

posted @ 2022-03-11 08:10  韩梦芫  阅读(297)  评论(0编辑  收藏  举报