Html 创建音乐或视频的下载链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20956434/
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-29 00:22:30  来源:igfitidea点击:

Create download link for music or video

htmlaudiovideobrowserhref

提问by Yohanes AI

i have html file with video and audio file. and i want to links file such as mp3 or mp4 using tag a href and download that file. My video and audio file stored in same folder with my html file.

我有包含视频和音频文件的 html 文件。我想使用标签 a href 链接诸如 mp3 或 mp4 之类的文件并下载该文件。我的视频和音频文件与我的 html 文件存储在同一文件夹中。

i have tried this code :

我试过这个代码:

<a href="music/hermin.mp3" target="_blank">download</a>

but not downloaded my file, just open a new tab with a play pause controls in the center.

但没有下载我的文件,只需打开一个新标签,中间有一个播放暂停控件。

from this questioni get to add "download" to my href tag, but it is for modern browser. How about for old browser?

从这个问题我可以将“下载”添加到我的 href 标签中,但它适用于现代浏览器。旧浏览器怎么样?

how i can create a download link for my video/audio in html file and support for all browser (not only for modern browser)?

我如何在 html 文件中为我的视频/音频创建下载链接并支持所有浏览器(不仅适用于现代浏览器)?



thanks in advance and sorry for my bad English.



预先感谢并为我的英语不好而道歉。

回答by Emir Dupovac

You can try this. I've tried it, and it's working for me.

你可以试试这个。我试过了,它对我有用。

<a href="link/to/your/download/file" download> Download link </a>

回答by ELavicount

It depends of your browser settings and plugins however if you are using php you can do a script to download the file like this one:

这取决于您的浏览器设置和插件,但是如果您使用的是 php,您可以执行一个脚本来下载这样的文件:

<?php   
if (isset($_GET['file'])) { 
    $file = $_GET['file'] ;
        if (file_exists($file) && is_readable($file) && preg_match('/\.mp3$/',$file))  { 
            header('Content-type: application/mp3');  
            header("Content-Disposition: attachment; filename=\"$file\"");   
            readfile($file); 
        } 
    } else { 
    header("HTTP/1.0 404 Not Found"); 
    echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>"; 
} 
?>

save it as download.php

保存为download.php

then create a link like this one

然后创建一个这样的链接

<html>
<body>
<a href="download.php?file=test.mp3">download</a>
</body>    
</html>

It should work now, have a nice day.

它现在应该工作了,祝你有美好的一天。