【问题标题】:Download mp3 file from android browser从安卓浏览器下载mp3文件
【发布时间】:2015-04-15 06:25:22
【问题描述】:

我参考了这些问题并实施了他们的答案,但没有一个对我有用。

download mp3 instead of playing in browser by default?

PHP Streaming MP3

content type for mp3 download response

download mp3 from web url with android failed

基本上当用户点击网站上的下载链接时,我们希望用户被提示下载音频文件。

代码:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");         
header("Content-Disposition: attachment; filename=".$file_name);
readfile($fname);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

这里,$mtype"audio/mpeg"

成功:

  1. 它在所有桌面浏览器上都能正常工作。
  2. 我尝试使用 BlackBerry,并获得了下载的文件。

失败:

  1. 当我在安卓浏览器上做同样的事情时,我没有得到下载的文件。
  2. 文件名也丢失,带有<untitled> 并显示状态"download unsuccessful"

感谢您知道为什么此代码无法在 Android 手机上运行。

【问题讨论】:

  • 首先,在执行readfile之前尝试发送标头。
  • @N.B.,我也尝试过,但没有成功。

标签: php android-browser


【解决方案1】:

我刚刚找到了这个问题的解决方案。在我的安卓小米上,它完美运行。

if (file_exists($file)) {
    echo "OK";
    if (ob_get_level()) {
  ob_end_clean();
} // clearing output script buffer

$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
header('Content-Description: File Transfer');
header('Content-Type: {$mime_type}');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

【讨论】:

    【解决方案2】:

    遇到同样的问题,可能是您的服务器不允许您正确使用 mp3 文件(我使用 Biz.nf)

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2010-12-19
      • 2012-02-25
      • 2012-01-05
      • 1970-01-01
      • 2011-08-28
      • 2021-02-26
      相关资源
      最近更新 更多