【问题标题】:Download file without right click无需右键下载文件
【发布时间】:2012-02-06 18:15:23
【问题描述】:

我在指向视频文件 (mp4) 的页面上有一个 <a href> 链接。当访问者点击链接时,视频文件会在浏览器窗口中打开。

如果你想下载文件,你必须“右键单击”,然后“将链接另存为...”

我希望这样可以在单击时下载文件(这样我就不必右键单击)。

编辑:这里有什么问题?

<?php
    header('Content-Description: File Transfer');
    header('Content-Type: video/mp4');
    header('Content-Disposition: attachment; filename=file');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
?>

【问题讨论】:

    标签: html download right-click save-as


    【解决方案1】:

    Content-Disposition header 设置为attachment。例如:

    Content-Disposition: attachment; filename=lolcats42.mp4
    

    【讨论】:

    • 我还有一个问题,文件大小为0字节,并且无法正常工作。源代码在编辑中
    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 2011-02-05
    • 2017-08-26
    • 2018-07-09
    • 2022-07-19
    • 2021-03-04
    • 1970-01-01
    相关资源
    最近更新 更多