H5上传前预览视频(结合 video标签 &&h5 fileApi)

2017/09/14 发布

 

js代码:

// hTML5实现表单内的上传文件框,上传前预览视频,刷新预览video,使用HTML5 的File API,
// 建立一个可存取到该file的url,一个空的video标签,ID为video0,把选择的文件显示在video标签中,实现视频预览功能。
// 需要选择支持HTML API的浏览器。
         $("#video").change(function(){
             var objUrl = getObjectURL(this.files[0]) ;
             console.log("objUrl = "+objUrl) ;
             if (objUrl) {
                 $("#video0").attr("src", objUrl) ;
             }
         }) ;
         //建立一个可存取到该file的url
         function getObjectURL(file) {
             var url = null ;
             if (window.createObjectURL!=undefined) { // basic
                 url = window.createObjectURL(file) ;
             } else if (window.URL!=undefined) { // mozilla(firefox)
                 url = window.URL.createObjectURL(file) ;
             } else if (window.webkitURL!=undefined) { // webkit or chrome
                 url = window.webkitURL.createObjectURL(file) ;
             }
             return url ;
         }

html:

<video style="height:auto;" src="" id="video0" controls="controls"></video>
<input class="form-control" type="file" style="height:auto;"
id="video" name="video"/>
posted @ 2018-02-25 11:10  mithrandirw  阅读(14988)  评论(0编辑  收藏  举报