Download HTML5 mp4 video using Javascript

Hi, I have an HTML5 video tag, I want to give the user a “Download” button where the User can click and download this video. I know that a user can download videos by right-clicking on browsers, but I want this feature to appear on my button.

I use simple code

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

but it redirects me to a video that does not show the download popup that I want.

thanks

+6
source share
3 answers

HTML5 download <a> DOM. JavaScript.

: fooobar.com/questions/77175/...

, PHP -

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

:

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

});
+2

Based on the answer of the Meganians:

header("Content-disposition: attachment; filename=" . basename($filename) .'"');
header('Content-type: application/octet-stream');
readfile($filename);'

Thanks, Megan. I copied and modified your code.

0
source

Source: https://habr.com/ru/post/1619154/


All Articles