简体   繁体   English

如何从 javascript/angular 触发 html5 视频标签下载事件

[英]How to trigger the html5 video tag download event from javascript/angular

We are currently using the HTML5 video tag to show a video on our site, which the user should be able to download if they want to keep it for future use.我们目前正在使用 HTML5 video标签在我们的网站上显示视频,如果用户想保留以供将来使用,应该能够下载该视频。 The HTML5 video tag provides a default option to download the video (marked by a black border in the image), however, we are required to provide additional control to let the user do the download (marked by a red border in the image.) HTML5 video标签提供了下载视频的默认选项(在图像中用黑色边框标记),但是,我们需要提供额外的控制来让用户进行下载(在图像中用红色边框标记)。 在此处输入图像描述

Is there a way to trigger the event that html5 video uses to do the download from javascript/angular?有没有办法触发 html5 视频用于从 javascript/angular 进行下载的事件?

you can use file-saver module to download a blob file npm-file saver module您可以使用 file-saver 模块下载 blob 文件npm-file saver 模块

After a little research I've found a solution, but it is needed to create a link instead of button to make it work.经过一番研究,我找到了一个解决方案,但需要创建一个链接而不是按钮才能使其工作。 You just need to point to your video resource in that link, open it in new tab/window and add download attribute to it:您只需要在该链接中指向您的视频资源,在新选项卡/窗口中打开它并为其添加download属性:

<a href="https://example.com/link-to-res.mp4" target="_blank" download>dl link</a>

You can also try to create this link and open it programmatically inside onclick function on your download button:您也可以尝试创建此链接并在下载按钮上的 onclick function 中以编程方式打开它:

clickFunction() {
    const a = document.createElement('a');
    a.href = "https://example.com/link-to-res.mp4";
    a.download = "";
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
}

Hope that will help out with your problem.希望对您的问题有所帮助。

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

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