【问题标题】:How to preview the video file that user wants to upload on the website如何在网站上预览用户要上传的视频文件
【发布时间】:2021-08-01 02:56:51
【问题描述】:

<div class="image-preview" id="imagePreview">
            <img src="" class="image-preview__image">
          </div>

          <div class="image-preview" id="videoPreview">
            <video controls autoplay muted class="video-preview__image" id="video" src=""></video>
          </div>

        <br>
        <label for="inpFile"><img id="image_icon" src="http://localhost/mybook/images/images_icon.png"></label>
        <input id="inpFile" type="file" name="file" style="display:none;" >
        <input id="post_button" type="submit" value="Post" style="margin:5px;">

我想在提交前显示上传视频文件的预览。多亏了以下 JS,我已经成功地使用图像完成了此操作,但它不适用于视频文件...

const inpFile = document.getElementById("inpFile");
  const previewContainer = document.getElementById("imagePreview");
  const originalImage = document.getElementById("originalImage");
  const previewImage = previewContainer.querySelector(".image-preview__image");

  inpFile.addEventListener("change", function() {
    const file = this.files[0];

    if (file) {
      const reader = new FileReader();

      previewContainer.style.display = "block";
      previewImage.style.display = "block";
      originalImage.style.display = "none";

      reader.addEventListener("load", function() {
        previewImage.setAttribute("src", this.result);
      });

      reader.readAsDataURL(file);
    }else{
      previewImage.style.display = null;
      previewImage.setAttribute("src","");
    }
  });

【问题讨论】:

标签: javascript


【解决方案1】:

nikoss 的回答可能对您有所帮助。

图片使用img,视频使用video

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 2011-05-25
    • 2019-02-02
    • 1970-01-01
    • 2020-02-14
    • 2015-10-07
    • 2020-02-02
    • 2016-01-29
    • 2021-02-17
    相关资源
    最近更新 更多