JavaScript: Preview video before uploading them on the server.

/ Updated : / 0 Comments

Preview videos before upload: Here in this article we will learn how to preview videos at the client-side before uploading them on the server via javascript. In our previous article, we have also learned how to preview images before upload. Preview videos before upload help to reduce our server load. Also, this allows us to upload valid videos by marking them as valid or final upload.

Steps to preview videos before upload

  1. Html markup: Add input file type and Video tag
  2. JS code: Use URL.createObjectURL to preview video.

#Html markup: Add input file type, Video tag

Here we add an input file type and Video HTML tag on our webpage. Our html markup looks like as shown below

<input type='file'  id='videoUpload' />

<video width="320" height="240" controls>
  Your browser does not support the video tag.
</video>

#Javascript: Usage of URL.createObjectURL to preview video.

Here first we select our input file type by its id .i.e document.getElementById. Then will attach the onchange event to it. At the onchange event, we set video tag src as blobURL, which is URL.createObjectURL of selected video file. The final javascript code to preview video looks like as shown below.

document.getElementById("videoUpload")
.onchange = function(event) {
  let file = event.target.files[0];
  let blobURL = URL.createObjectURL(file);
  document.querySelector("video").src = blobURL;
}


View Demo

Finally we able to preview the selected videos before uploading them to the server.

Other Reference:

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to do a favor, then Buy me a coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

0 Comments