一、前言

  项目中需要提供一个视频介绍,使用户能够快速、方便的了解如何使用产品以及注意事项。

  前台使用vue+element ui中的el-upload组件实现视频上传及进度条展示,后台提供视频上传api并返回url。

二、具体实现

1、效果图展示 

                                      

2、html代码

<div class="album albumvideo">
    <div>
        <p class="type_title">
            <span>视频介绍</span>
        </p>
        <div class="pic_img">
            <div class="pic_img_box">
                <el-upload class="avatar-uploader"
                           action="上传地址"
                           v-bind:data="{foldpath:'上传目录',secretkey:'安全验证'}"
                           v-bind:on-progress="uploadvideoprocess"
                           v-bind:on-success="handlevideosuccess"
                           v-bind:before-upload="beforeuploadvideo"
                           v-bind:show-file-list="false">
                    <video v-if="videoform.showvideopath !='' && !videoflag"
                           v-bind:src="videoform.showvideopath"
                           class="avatar video-avatar"
                           controls="controls">
                        您的浏览器不支持视频播放
                    </video>
                    <i v-else-if="videoform.showvideopath =='' && !videoflag"
                       class="el-icon-plus avatar-uploader-icon"></i>
                    <el-progress v-if="videoflag == true"
                                 type="circle"
                                 v-bind:percentage="videouploadpercent"
                                 style="margin-top:7px;"></el-progress>
                </el-upload>
            </div>
        </div>
    </div>
    <p class="upload_pictures">
        <span></span>
        <span>最多可以上传1个视频,建议大小50m,推荐格式mp4</span>
    </p>
</div>

3、js代码

<script>
    var vm = new vue({
        el: '#app',
        data: {
            videoflag: false,
            //是否显示进度条
            videouploadpercent: "",
            //进度条的进度,
            isshowuploadvideo: false,
            //显示上传按钮
            videoform: {
                showvideopath: ''
            }
        },
        methods: {
            //上传前回调
            beforeuploadvideo(file) {
                var filesize = file.size / 1024 / 1024 < 50;
                if (['video/mp4', 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb', 'video/mov'].indexof(file.type) == -1) {
                    layer.msg("请上传正确的视频格式");
                    return false;
                }
                if (!filesize) {
                    layer.msg("视频大小不能超过50mb");
                    return false;
                }
                this.isshowuploadvideo = false;
            },
            //进度条
            uploadvideoprocess(event, file, filelist) {
                this.videoflag = true;
                this.videouploadpercent = file.percentage.tofixed(0) * 1;
            },
            //上传成功回调
            handlevideosuccess(res, file) {
                this.isshowuploadvideo = true;
                this.videoflag = false;
                this.videouploadpercent = 0;
                
                //前台上传地址
                //if (file.status == 'success' ) {
                //    this.videoform.showvideopath = file.url;
                //} else {
                //     layer.msg("上传失败,请重新上传");
                //}

                //后台上传地址
                if (res.code == 0) {
                    this.videoform.showvideopath = res.data;
                } else {
                    layer.msg(res.message);
                }
            }
        }
    })
</script>

4、后台代码

/// <summary>
/// 上传视频
/// </summary>
/// <returns></returns>
[httppost]
public ihttpactionresult uploadvideo()
{
    try
    {
        var secretkey = httpcontext.current.request["secretkey"];
        if (secretkey == null || !_secretkey.equals(secretkey))
            return ok(new result(-1, "验证身份失败"));
        var files = httpcontext.current.request.files;
        if (files == null || files.count == 0)
            return ok(new result(-1, "请选择视频"));
        var file = files[0];
        if (file == null)
            return ok(new result(-1, "请选择上传的视频"));
        //存储的路径             
        var foldpath = httpcontext.current.request["foldpath"];
        if (foldpath == null)
            foldpath = "/upload";
        foldpath = "/uploadfile" + "/" + foldpath;
        if (foldpath.contains("../"))
            foldpath = foldpath.replace("../", "");
        //校验是否有该文件夹 
        var mappath = appdomain.currentdomain.basedirectory + foldpath;
        if (!directory.exists(mappath))
            directory.createdirectory(mappath);
        //获取文件名和文件扩展名
        var extension = path.getextension(file.filename);
        if (extension == null || !".ogg|.flv|.avi|.wmv|.rmvb|.mov|.mp4".contains(extension.tolower()))
            return ok(new result(-1, "格式错误"));

        string newfilename = guid.newguid() + extension;
        string absolutepath = string.format("{0}/{1}", foldpath, newfilename);
        file.saveas(appdomain.currentdomain.basedirectory + absolutepath);
       
        string fileurl = string.format("{0}://{1}{2}", httpcontext.current.request.url.scheme, httpcontext.current.request.url.authority, absolutepath);
        return json(new resultdata(0, "success",fileurl));
    }
    catch (exception e)
    {
        logger.error("uploadvideo is error", gettype(), e);
        return json(new result(-1, "上传失败"));
    }
}

三、总结

  注意:web.config文件配置之限制上传文件大小和时间的属性配置(1kb=1024b 1mb=1024kb 1gb=1024mb)

     

  在web.config文件中配置限制上传文件大小与时间字符串时,是在<httpruntime><httpruntime/>节中完成的,需要设置以下2个属性:

  • maxrequestlength属性:该限制可用于防止因用户将大量文件传递到该服务器而导致的拒绝服务攻击。指定的大小以kb为单位,默认值为4096kb(4mb)。
  • executiontimeout属性:指定在asp.net应用程序自动关闭前,允许执行请求的最大秒数。单位为秒,默认值为110s。 

 

优秀是一种习惯,欢迎大家关注学习