简体   繁体   English

使用Javascript下载HTML5 mp4视频

[英]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. 嗨,我有HTML5的视频标签,我想给用户一个“下载”按钮,用户可以点击并下载该视频。 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 谢谢

HTML5 browsers now allow you to add the download attribute to <a> tags to achieve this in your DOM. HTML5浏览器现在允许您将download属性添加到<a>标签以在DOM中实现此目的。 You cannot do this in pure javascript. 你不能在纯JavaScript中这样做。

Source: https://stackoverflow.com/a/6794432/5203655 资料来源: https//stackoverflow.com/a/6794432/5203655

If however, you have access to the server response, then in PHP you could do something like 但是,如果您可以访问服务器响应,那么在PHP中您可以执行类似的操作

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

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

});

Based on Meghans answer: 根据Meghans的回答:

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

Thanks, @Meghan. 谢谢,@ Meghan。 I copied and changed your code. 我复制并更改了你的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM