Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set preview of video file, selecting from input type='file'

In one of my module, I need to browse video from input[type='file'], after that I need to show selected video before starting upload.

I am using basic HTML tag to show. but it is not working.

Here is code:

$(document).on("change",".file_multi_video",function(evt){        var this_ = $(this).parent();    var dataid = $(this).attr('data-id');    var files = !!this.files ? this.files : [];    if (!files.length || !window.FileReader) return;         if (/^video/.test( files[0].type)){ // only video file      var reader = new FileReader(); // instance of the FileReader      reader.readAsDataURL(files[0]); // read the local file      reader.onloadend = function(){ // set video data as background of div                        var video = document.getElementById('video_here');            video.src = this.result;        }     }      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <video width="400" controls >                <source src="mov_bbb.mp4" id="video_here">              Your browser does not support HTML5 video.            </video>       <input type="file" name="file[]" class="file_multi_video" accept="video/*">
like image 763
Ronak Patel Avatar asked Mar 16 '16 12:03

Ronak Patel


People also ask

How do I preview a file in HTML?

For single html file, in VS 2022, you can click File > View in Browser (Ctrl + Shift + W) to preview it(or right-click the white space of this single html file > click View in Browser ). Besides, you can select File > Browse With… > Set as Default to change other browsers as the default browser.

How do I preview a selected image in HTML?

Step 1: Create a Basic Layout for the Image Preview Using HTML. Add a div element with a class named image-preview-container . Inside it, add another div element with a class named preview . This element will contain an img tag with an ID named preview-selected-image .

How do you assign the value of an input type file?

You can't. The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.


1 Answers

@FabianQuiroga is right that you should better use createObjectURL than a FileReader in this case, but your problem has more to do with the fact that you set the src of a <source> element, so you need to call videoElement.load().

$(document).on("change", ".file_multi_video", function(evt) {    var $source = $('#video_here');    $source[0].src = URL.createObjectURL(this.files[0]);    $source.parent()[0].load();  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <video width="400" controls>    <source src="mov_bbb.mp4" id="video_here">      Your browser does not support HTML5 video.  </video>    <input type="file" name="file[]" class="file_multi_video" accept="video/*">

Ps: don't forget to call URL.revokeObjectURL($source[0].src) when you don't need it anymore.

like image 60
Kaiido Avatar answered Sep 22 '22 22:09

Kaiido