• 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);
            }
  • 相关阅读:
    win8及win8.1商店出现0X80073CF9的解决办法!
    Ubuntu 14.04 登陆界面循环问题解决
    Java学习笔记-Json
    Java学习笔记-Thread-线程
    git学习笔记
    Java学习笔记-File
    java学习笔记-set
    C# 实验4 数据库
    C#文件处理
    C#-实验3
  • 原文地址:https://www.cnblogs.com/siyunianhua/p/15992115.html
Copyright © 2020-2023  润新知