download HTML5 mp4 video using Javascript

33,001

Solution 1

HTML5 browsers now allow you to add the download attribute to <a> tags to achieve this in your DOM. You cannot do this in pure javascript.

Source: https://stackoverflow.com/a/6794432/5203655

If however, you have access to the server response, then in PHP you could do something like

<?php
header('Content-type: application/octet-stream');
readfile('myvideo.mp4');

Solution 2

You can use this:

$('submit').click(function() {
  $('<a/>',{
     "href":"The/video/src/path",
    "download":"video.mp4",
    id:"videoDownloadLink"
  }).appendTo(document.body);
  $('#videoDownloadLink').get(0).click().remove();

});
Share:
33,001
Rohit
Author by

Rohit

Updated on August 05, 2022

Comments

  • Rohit
    Rohit over 1 year

    Hi I have video tag of HTML5, I want to give a "Download" button to user, where User can able to click and download that video. I know user can download video by right click on browsers, but i want to give this feature on my button.

    I am using simple code

    $('submit').click(function() {
        window.location.href = 'myvideo.mp4';
    });
    

    but it redirect me to video url not shows download popup that i want.

    Thanks