javascript 如何设置视频文件的预览,从输入类型='文件'中选择

oknrviil  于 2023-04-19  发布在  Java
关注(0)|答案(7)|浏览(223)

在我的一个模块中,我需要从input[type='file']浏览视频,之后我需要在开始上传之前显示选定的视频。
我使用基本的HTML标记来显示。但它不工作。
代码如下:

$(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/*">
f87krz0w

f87krz0w1#

@FabianQuiroga是对的,在这种情况下,你应该更好地使用createObjectURL而不是FileReader,但是你的问题更多地与你设置<source>元素的src有关,所以你需要调用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:不要忘记调用URL.revokeObjectURL($source[0].src)当你不需要它了。

p1tboqfb

p1tboqfb2#

别忘了它使用的是jquery库

Javascript

$ ("#video_p").change(function () {
   var fileInput = document.getElementById('video_p');
   var fileUrl = window.URL.createObjectURL(fileInput.files[0]);
   $(".video").attr("src", fileUrl);
});

Html

< video controls class="video" >
< /video >
7xzttuei

7xzttuei3#

这里有一个简单的peasy解决方案

document.getElementById("videoUpload").onchange = function(event) {
  let file = event.target.files[0];
  let blobURL = URL.createObjectURL(file);
  document.querySelector("video").style.display = 'block';
  document.querySelector("video").src = blobURL;
}
<input type='file'  id='videoUpload'/>

<video width="320" height="240" style="display:none" controls autoplay>
  Your browser does not support the video tag.
</video>

解决方案是使用vanilla js,所以你不需要使用jQuery,在chrome上测试和工作,好运

l5tcr1uw

l5tcr1uw4#

如果你正面临这个问题。那么你可以使用下面的方法来解决上述问题。
下面是HTML代码:

//input tag to upload file
<input class="upload-video-file" type='file' name="file"/>

//div for video's preview
 <div style="display: none;" class='video-prev' class="pull-right">
       <video height="200" width="300" class="video-preview" controls="controls"/>
 </div>

下面是JS函数:

$(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];
}
qq24tv8q

qq24tv8q5#

让它变得简单
HTML:

<video width="100%" controls class="myvideo" style="height:100%">
<source src="mov_bbb.mp4" id="video_here">
Your browser does not support HTML5 video.
</video>

JS:

function readVideo(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();   
        reader.onload = function(e) {
            $('.myvideo').attr('src', e.target.result);
        };

        reader.readAsDataURL(input.files[0]);
    }
}
aamkag61

aamkag616#

上传前预览视频使用jQuery

$(function(){
    $('#videoFileInput').on('change',function(){
        var file = this.files[0];
        var reader = new FileReader();
        reader.onload = viewer.load;
        reader.readAsDataURL(file);
    });
    var viewer = {
        load : function(e) {
            $('#preview').show();
            $('#preview').attr('src',e.target.result);
        },
    }
});
<input type="file" name="video_file" class="form-control" accept="video/mp4" id="videoFileInput">

<video id="preview" width="600" controls style="display: none;margin-top: 25px;">
  Your browser does not support HTML5 video.
</video>
6yt4nkrj

6yt4nkrj7#

以下是VUE JS的示例:preview PICTURE
Example SOURCE CODE - DRAG-DROP _part
Example with RENDERing & createObjectURL()使用VIDEO.js
p.s.我只是想改进“Pragya Sriharsh”解决方案:

const = isVideo = filename =>'m4v,avi,mpg,mov,mpg,mpeg'
.split(',')
.includes( getExtension(filename).toLowerCase() )

请不要使用jQuery,它现在是2k 19:-);

  • 〉所以:
const getExtension = filename => {
    const parts = filename.split('.');
    return parts[parts.length - 1];
}

...并让其余的工作将由Webpack 4完成!

相关问题