JavaScript: Preview video before uploading them on the server.


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.

#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 the selected video file.

The final javascript code to preview the video looks like as shown below.

.onchange = function(event) {
  let file =[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 thank me? 👳 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 *