利用bootstrap上传视频文件,mvc做后台处理

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    @Styles.Render("~/Content/MUI/css/MUICore")
    @Styles.Render("~/Content/MUI/css/MUIALL")
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <link href="~/Content/fileinput.css" rel="stylesheet" />
    <style type="text/css">
        label {
            font-size: small;
            font-weight: normal;
            padding: 4% 0 0 6% !important;
        }
    </style>
</head>
<body>
    <div>
        <label>
            <input type="file" id="file" multiple class="projectfile" data-overwrite-initial="false" data-min-file-count="1" />
        </label>
        <input id="Imgurl" hidden="hidden" />
        <input id="Imgname" hidden="hidden" />
    </div>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/MUIALL")
    <script src="~/Content/Scripts/bootstrap.js"></script>
    <script src="~/Content/Scripts/fileinput.js"></script>
    <script src="~/Content/Scripts/fileinput_locale_zh.js"></script>
    <script type="text/javascript" charset="utf-8">
        $(function () {
            var List = [];
            var imgulr = "";
            $("#file").fileinput({
                language: 'zh',
                uploadUrl: '../PlayVideo/SaveFile', //后台处理链接
                uploadExtraData: function (previewId, index) {   //额外参数的关键点
                    var obj = {};
                    obj.sqlpath = imgulr;
                    console.log(obj);
                    return obj;
                },
                uploadAsync: false,
                overwriteInitial: false,
                dropZoneEnabled: false,
                showPreview: true,
                showRemove: false,
                showUpload: false,
            }).on('filepreajax', function (event, previewId, index) {
                imgulr = $("#Imgurl").val();
            }).on("fileclear", function (event, data, msg) {
                List = [];
                $("#Imgurl").val("");
            }).on('filebatchselected', function (event, files) {//选中文件事件
                $(this).fileinput("upload");
            }).on("filebatchuploadsuccess", function (event, data, previewId, index) {
                $("#Imgurl").val(data.response);
                var name = data.response.split(';');  //文件上传成功返回的文件名,可返回自定义文件名
                List = [];
                for (var i = 0; i < name.length - 1; i++) {
                    var newname = name[i].split('/');
                    List.push({ FileName: "" + newname[2] + "" });
                }
            })
            $("#file").fileinput({ 'showUpload': false, 'previewFileType': 'any' });
            var opinionID = "";
            var urlhref = window.location.href;
            if (urlhref.indexOf("?") >= 0) {
                var urlt = urlhref.split("?");
                var gets = urlt[1].split("&");
                for (var i = 0; i < gets.length; i++) {
                    var get = gets[i].split("=");
                    if (get[0] == "opinionID") {
                        opinionID = get[1];
                        break;
                    }
                }
            }
        });
    </script>
</body>
</html>

上面是前端页面,下面添加后台处理程序

/// <summary>
/// 保存上传文件
/// </summary>
/// <param name="sqlpath">路径</param>
/// <returns></returns>
public ActionResult SaveFile(string sqlpath)
{
//获取前端传过来的文件
var files = HttpContext.Request.Files;
//设置上传文件存储的文件名
string str = "files";
//获取当前程序运行的物理地址
string uploadPath = HttpRuntime.AppDomainAppPath.ToString() + str + "\\";
for (int i = 0; i < files.Count; i++)
{
HttpPostedFileBase file = files[i];
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
string name = file.FileName;
string ext = Path.GetExtension(name);
string newname = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ext;
sqlpath = "../" + str + "/" + newname; 
string fullName = uploadPath + newname;
file.SaveAs(fullName);
//string len1 = GetMediaTimeLen(fullName);
//获取视频的秒数
int videoSeconds = GetMediaTimeLenSecond(fullName); 
//往数据库里面进行存储 
videoSave vs = new videoSave();
vs.guid = Guid.NewGuid().ToString();
vs.videoPath = sqlpath;
vs.videoLengthTime = videoSeconds;
vs.videoHasTime = 0;
vs.videoStatus = "未观看";
db.Entry(vs).State = EntityState.Added;
db.SaveChanges();
}
}
return Json(sqlpath, JsonRequestBehavior.AllowGet);
}

下面这个是获取视频文件总时长的方法,按照秒数进行计算,计算出一共多少秒,返回   “15”   类似的数据  注:这个例子是使用的这个方法

/// <summary>
        /// 获取视频的秒数
        /// </summary>
        /// <param name="path">该视频对应的路径</param>
        /// <returns></returns>
        public static int GetMediaTimeLenSecond(string path)
        {
            System.Type objType = System.Type.GetTypeFromProgID("Shell.Application");
            dynamic shell = System.Activator.CreateInstance(objType);
            //文件路径
            dynamic folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
            //文件名称
            dynamic folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
            string len;
            if(Environment.OSVersion.Version.Major>=6)
            {
                len = folder.GetDetailsOf(folderitem, 27);
            }
            else
            {
                len = folderitem.GetDetailsOf(folderitem, 21);
            }
            string[] str = len.Split(new char[] { ':' });
            int sum = 0;
            sum = int.Parse(str[0]) * 60 * 60 + int.Parse(str[1]) * 60 + int.Parse(str[2]);
            return sum;
        }

再添加一个获取视频文件总时长的方法,会返回“00:00:12”类似的数据

/// <summary>
        /// 获取视频时长
        /// </summary>
        /// <param name="path">该视频对应的路径</param>
        /// <returns></returns>
        public static string GetMediaTimeLen(string path)
        {
            System.Type objType = System.Type.GetTypeFromProgID("Shell.Application");
            dynamic shell = System.Activator.CreateInstance(objType);
            //文件路径
            dynamic folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
            //文件名称
            dynamic folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
            if(Environment.OSVersion.Version.Major>=6)
            {
                return folder.GetDetailsOf(folderitem, 27);
            }
            else
            {
                return folder.GetDetailsOf(folderitem, 21);
            }
        }

 

 

 

运行结果:

 

 

 

 

 

存储到数据库中:

 

 新人笔记,防止以后遗忘,如有问题,请留言,不胜感激

 
posted @ 2018-11-15 14:44  SAS、A  阅读(505)  评论(0编辑  收藏  举报