使用 Javascript 下载 HTML5 mp4 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34172675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:59:58  来源:igfitidea点击:

download HTML5 mp4 video using Javascript

javascriptjqueryhtmlvideohtml5-video

提问by Rohit

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.

嗨,我有 HTML5 的视频标签,我想给用户一个“下载”按钮,用户可以在其中单击并下载该视频。我知道用户可以通过右键单击浏览器来下载视频,但我想在我的按钮上提供此功能。

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.

但它将我重定向到视频 url 没有显示我想要的下载弹出窗口。

Thanks

谢谢

采纳答案by Meghan

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

HTML5 浏览器现在允许您将download属性添加到<a>标签以在 DOM 中实现这一点。您不能在纯 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');

回答by Jai

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

});

回答by Chirag Arora

Based on Meghans answer:

基于梅根的回答:

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

Thanks, @Meghan. I copied and changed your code.

谢谢,@梅根。我复制并更改了您的代码。