1. 程式人生 > 實用技巧 >video上傳本地預覽

video上傳本地預覽

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"/>