默认情况下下载 mp3 而不是在浏览器中播放? [英] download mp3 instead of playing in browser by default?

查看:15
本文介绍了默认情况下下载 mp3 而不是在浏览器中播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,用户可以在其中从 Flash 播放器流式传输我的音乐或下载单独的歌曲(作为 mp3).现在,如果您单击下载链接,它们只会在浏览器中播放.我可以这样做吗,默认情况下会弹出下载框,而无需压缩文件使用户 rt.txt ?点击?

我有我的 index.php 页面,其中的链接看起来像这样(从 mySQL 动态提取文件名:

...<td><?php 包括 'Media/' .$row['type'] .'/' .$row['folder_name'] .'/download.php' ?></td>

然后download.php有这个:

<h3>下载:</h3><ul><li><a href="auto_download.php?path=Media/<?php echo $row['type'] .'/' . $row['folder_name'] ?>/Chemtengure.mp3">Chemtengure</a>

我把 auto_download.php 和 index.php 放在同一个目录下:

auto_download.php:

$path = $_GET['path'];header('Content-Disposition: 附件;文件名 ='.basename($path));读取文件($路径);

解决方案

您需要将此 HTTP 标头添加到您的响应中,以使浏览器强制下载:

Content-Disposition:附件;文件名=您的文件名.mp3

例如,如果您在服务器端使用 PHP,您可以创建一个脚本,发送上述标头后跟实际文件内容:

$path = $_GET['path'];header('Content-Disposition: 附件;文件名 ='.basename($path));读取文件($路径);

如果你调用这个脚本download.php,那么链接到download.php?path=/path/to/your/file.mp3 将使浏览器弹出- 为 file.mp3 设置一个下载对话框.

I have a site where users can stream my music from a flash player or download the individual songs (as mp3s). Right now if you click on the download links, they just play in the browser. Can I make it so the download box will pop up by default without either zipping the files making the user rt. click?

I have my index.php page with the links looking like this (pulling file names dynamically from mySQL:

<table>
.
.
.
<td>
    <?php include 'Media/' . $row['type'] . '/' . $row['folder_name'] . '/download.php' ?>
</td>

and then download.php has this:

<div class="downloadCell">
    <h3>Downloads:</h3>
    <ul>
        <li>
          <a href="auto_download.php?path=Media/<?php echo $row['type'] . '/' . $row['folder_name'] ?>/Chemtengure.mp3">Chemtengure</a>
        </li>
    </ul>
</div>

and I put the auto_download.php in the same directory as index.php:

auto_download.php:

$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);

解决方案

You will need to add this HTTP header to your response to make the browser force a download:

Content-Disposition: attachment; filename=your_filename.mp3

For example, if you're using PHP on the server side, you can create a script that sends the above header followed by the actual file contents:

$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);

If you call this script download.php, then linking to download.php?path=/path/to/your/file.mp3 will make the browser pop-up a download dialog for file.mp3.

这篇关于默认情况下下载 mp3 而不是在浏览器中播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆