If you run into this problem. Then you can use the method below to solve the above problem.
Here is the HTML code:
//input tag to upload file <input class="upload-video-file" type='file' name="file"/> //div for video preview <div style="display: none;" class='video-prev' class="pull-right"> <video height="200" width="300" class="video-preview" controls="controls"/> </div>
The following is the JS function:
$(function() { $('.upload-video-file').on('change', function(){ if (isVideo($(this).val())){ $('.video-preview').attr('src', URL.createObjectURL(this.files[0])); $('.video-prev').show(); } else { $('.upload-video-file').val(''); $('.video-prev').hide(); alert("Only video files are allowed to upload.") } }); }); // If user tries to upload videos other than these extension , it will throw error. function isVideo(filename) { var ext = getExtension(filename); switch (ext.toLowerCase()) { case 'm4v': case 'avi': case 'mp4': case 'mov': case 'mpg': case 'mpeg': // etc return true; } return false; } function getExtension(filename) { var parts = filename.split('.'); return parts[parts.length - 1]; }
Pragya sriharsh
source share