Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download HTML5 mp4 video using Javascript

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

like image 901
Rohit Avatar asked Dec 09 '15 07:12

Rohit


2 Answers

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');
like image 147
Meghan Avatar answered Sep 29 '22 08:09

Meghan


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();

});
like image 20
Jai Avatar answered Sep 29 '22 08:09

Jai