Beispiel #1
0
        public async System.Threading.Tasks.Task <ActionResult> UploadVedioAsync()
        {
            try
            {
                var file = System.Web.HttpContext.Current.Request.Files[0];
                if (file.InputStream.Length == 0)
                {
                    return(Json(new { isok = false, msg = "上传失败,视频大小不能空", Path = "" }, JsonRequestBehavior.AllowGet));
                }

                if (file.InputStream.Length > 20971520)
                {
                    return(Json(new { isok = false, msg = "上传失败,视频文件不能超过20MB", Path = "" }, JsonRequestBehavior.AllowGet));
                }

                string   fileType  = System.IO.Path.GetExtension(file.FileName).ToLower();
                string[] VideoType = new string[] { ".mpeg4", ".mp4", ".mov", ".avi", ".wmv", ".3gp" };
                if (!VideoType.Contains <string>(fileType.ToLower()))
                {
                    return(Json(new { isok = false, msg = "不支持的格式,目前视频格式暂支持.mpeg4,.mp4,.mov,.avi,.wmv,.3gp" }, JsonRequestBehavior.AllowGet));
                }


                byte[] byteData = new byte[file.InputStream.Length];

                string tempurl    = string.Empty;
                string tempkey    = VideoAliMtsHelper.GetOssVideoKey(fileType.Replace(".", ""), true, out tempurl);
                Stream fileStream = file.InputStream;
                if (null != fileStream)
                {
                    using (System.IO.BinaryReader br = new System.IO.BinaryReader(fileStream))
                    {
                        byteData = br.ReadBytes(file.ContentLength);
                    }
                }
                if (null == byteData)
                {
                    log4net.LogHelper.WriteInfo(this.GetType(), "视频获取byte[]失败!");
                    return(Json(new { isok = false, msg = "系统错误!请联系管理员." }));
                }
                bool putrsult = AliOSSHelper.PutObjectFromByteArray(tempkey, byteData, 1, fileType.ToLower());
                Tuple <string, string> pathDic = await C_AttachmentVideoBLL.SingleModel.GetConvertVideoPathAsync(tempkey);

                return(Json(new { isok = true, msg = "上传成功", videoconvert = pathDic.Item2, Vid = 0, soucepath = pathDic.Item1 }));
            }
            catch (Exception ex)
            {
                return(Json(new { isok = false, msg = "系统错误,请重新尝试" + ex.Message, Path = "" }));
            }
        }
Beispiel #2
0
        /// <summary>
        /// 此方法由APP调用。不可删除
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            //因图文混排视频直接显示,设两个返回的临时视频地址
            var reVideoUrl = string.Empty;
            var rePostUrl  = string.Empty;

            context.Response.ContentType = "text/html";
            string         name = context.Request["name"];
            HttpPostedFile file = context.Request.Files[name];

            try
            {
                int minsnsId = 0;
                int.TryParse(context.Request["minsnsId"], out minsnsId);
                var           tempurl       = string.Empty;
                string        fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();
                int           flag          = 0;//标志是否需要转换
                List <string> extents       = new List <string> {
                    ".mp4", ".ogg", ".mpeg4", ".webm", ".MP4", ".OGG", ".MPEG4", ".WEBM"
                };
                string posterUploadDirectory = Path.Combine(ConfigurationManager.AppSettings["ImgUploadUrl4"], "videoposter", "mp4", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString());

                var tempkey    = VideoAliMtsHelper.GetOssVideoKey(fileExtension.Replace(".", ""), true, out tempurl);
                var byteData   = new byte[file.ContentLength];
                var fileStream = file.InputStream;
                if (null != fileStream)
                {
                    using (System.IO.BinaryReader br = new System.IO.BinaryReader(fileStream))
                    {
                        byteData = br.ReadBytes(file.ContentLength);
                    }
                }
                if (null == byteData)
                {
                    log4net.LogHelper.WriteInfo(this.GetType(), "视频获取byte[]失败!");
                    context.Response.Write(new JavaScriptSerializer().Serialize(new { result = 0, msg = "系统错误!请联系管理员." }));
                }
                var putrsult = AliOSSHelper.PutObjectFromByteArray(tempkey, byteData, 1, fileExtension.Replace(".", ""));
                if (extents.Contains(fileExtension))
                {
                    flag = 1;
                }

                VideoAttachment video = new VideoAttachment();
                video.CreateDate = DateTime.Now;
                video.VideoSize  = (file.ContentLength / (1024 * 1024.0)).ToString("N2");//单位为M
                //无需转换
                if (1 == flag)
                {
                    video.Status          = 1;
                    video.ConvertFilePath = VideoAliMtsHelper.GetUrlFromKey(tempkey.Replace("temp/", ""));
                }
                else
                {
                    var regex = new Regex(@"(?i)\.[\S]*");
                    video.ConvertFilePath = VideoAliMtsHelper.GetUrlFromKey(regex.Replace(tempkey.Replace("temp/", ""), ".mp4"));
                    video.Status          = -2;//待转换
                }
                video.UserId         = int.Parse(context.Request.Form["UId"] ?? "0");
                video.Fid            = minsnsId;
                video.SourceFilePath = tempkey;
                int Vid = Convert.ToInt32(VideoAttachmentBLL.SingleModel.Add(video));
                context.Response.Write(new JavaScriptSerializer().Serialize(new { result = "ok", msg = "上传成功", Vid = Vid }));
            }
            catch (Exception ex)
            {
                StringHelper.WriteOperateLog("error", ex.Message);
                context.Response.Write("");
            }
        }