阻止浏览器在新浏览器窗口中播放 mp3

发布于 2025-01-07 01:28:32 字数 255 浏览 1 评论 0原文

有没有办法阻止浏览器在新的浏览器窗口中打开 mp3?

我有一个 mp3 文件的正常链接

 <a href="link_to_mp3">some.mp3</a>

,当然您可以右键单击 -> 下载它另存为。但有些用户不知道。因此,如果他们单击链接,则会打开一个新窗口,他们可以在其中收听该 mp3。

问题是,我希望用户下载文件一次,而不是在我的服务器上收听它们数千次;)

is there a way to prevent a browser from opening an mp3 in a new browser window?

i have a normal link to mp3 file like

 <a href="link_to_mp3">some.mp3</a>

and of course you can download it with right click -> save as. but some users don't know that. so if they click on a link, a new window opens where they can listen to that mp3.

issue is, i want the users to download the files once instead of listen to them thousand times over my server ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2025-01-14 01:28:32

仅通过 html,您无法强制链接打开文件保存对话框。

相反,您可以通过 PHP 和一些自定义标头来实现这一点 如此处所示

一个简单的 download.php 文件可能如下所示:

    <?php
     
     $file = $_GET['file'];
     $dir = "path/to/files/";
     
     if(!file)
     {
         die('file not found');
     }
     else
     {
         $local_file = $dir . $file['filename'];
         $file = fopen($local_file, "r");  


         header("Cache-Control: public");
         header('Content-Type: application/octet-stream'); 
         header("Content-Description: File Transfer");
         header("Content-Disposition: attachment; filename=$file");
         header("Content-Transfer-Encoding: binary");
        
         // set download rate
         $download_rate = 100.0;
         // fetch the file
         fread($file, round($download_rate * 1024)); 
         // close the file stream
         fclose($file);
     }
     ?>

您可以在其中指定保存所有文件的目录,以及下载速率(如果需要限制文件下载的速度) 。

更新

忘记提及您将更改链接到:

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

其中 test.mp3 将更改为您的特定下载

Through html only you cannot force the link to open a file save dialog.

Instead you can achieve that through PHP and with some custom headers as seen here

A simple download.php file could look like:

    <?php
     
     $file = $_GET['file'];
     $dir = "path/to/files/";
     
     if(!file)
     {
         die('file not found');
     }
     else
     {
         $local_file = $dir . $file['filename'];
         $file = fopen($local_file, "r");  


         header("Cache-Control: public");
         header('Content-Type: application/octet-stream'); 
         header("Content-Description: File Transfer");
         header("Content-Disposition: attachment; filename=$file");
         header("Content-Transfer-Encoding: binary");
        
         // set download rate
         $download_rate = 100.0;
         // fetch the file
         fread($file, round($download_rate * 1024)); 
         // close the file stream
         fclose($file);
     }
     ?>

Where you can specify the directory where all your files as saved, as well as a download rate if needed to limit the speed at which the file can be downloaded.

Update

Forgot to mention that you will then change your links to:

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

where test.mp3 will be changed to your particular download

深爱成瘾 2025-01-14 01:28:32

快速而肮脏:也许你将其压缩...然后浏览器会自动下载该文件

quick and dirty: maybe you .zip it...than the browser automatically downloads the file

临走之时 2025-01-14 01:28:32

谢谢你的剧本。它对我来说不起作用,因为 $local_file 变量构建不正确..不要问,不是真正的 php 专业人士。你的帖子仍然让我朝着正确的方向前进。这是我的解决方案

<?php
$downloadfile = "directory/".$_GET['file'];
$filename = $_GET['file'];
$filesize = filesize($downloadfile);

header("Content-Type: audio/mpeg3"); 
header("Content-Disposition: attachment; filename=$filename"); 
header("Content-Length: $filesize");

readfile($downloadfile);
exit;
?>

哦,对于其他 php 傻瓜:脚本开头没有空格!

thanks for your script. it didn t worked out for me, because the $local_file variable wasn't build right.. don t ask, not really a php pro. still your post put me in the right direction. here s my solution

<?php
$downloadfile = "directory/".$_GET['file'];
$filename = $_GET['file'];
$filesize = filesize($downloadfile);

header("Content-Type: audio/mpeg3"); 
header("Content-Disposition: attachment; filename=$filename"); 
header("Content-Length: $filesize");

readfile($downloadfile);
exit;
?>

oh, and for other php-dummies: NO WHITESPACE AT THE BEGINNING OF THE SCRIPT!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文