使链接提示“另存为”的简单方法

发布于 2024-12-19 22:23:04 字数 59 浏览 0 评论 0原文

是否有一种快速内联方式,可以使我的某些链接在第一次初始单击后提示“文件另存为”(与右键单击相同的屏幕)?

Is there a quick inline way, to make some of my links prompt 'save file as' (Same screen as right click) after the first initial click?

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

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

发布评论

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

评论(2

梦中楼上月下 2024-12-26 22:23:04

您只需要发送适当的标头即可。 Content-Disposition 是关键(请参阅 http://en.wikipedia.org/维基/MIME)。

请参阅下面的 PHP 示例。

/**
 * @author Gajus Kuizinas <[email protected]>
 * @copyright Anuary Ltd, http://anuary.com
 * @version 1.0.1 (2011 11 18)
 */
function ay_file_force_download($file, $file_name = NULL)
{
    if(headers_sent())
    {
        throw new Ay_Exception('Headers have been already sent. Cannot force file download.');
    }

    $file_name  = $file_name === NULL ? basename($file) : $file_name;

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $file_name);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

    exit;
}

You simply need to send appropriate headers. Content-Disposition is the key (see http://en.wikipedia.org/wiki/MIME).

See PHP example below.

/**
 * @author Gajus Kuizinas <[email protected]>
 * @copyright Anuary Ltd, http://anuary.com
 * @version 1.0.1 (2011 11 18)
 */
function ay_file_force_download($file, $file_name = NULL)
{
    if(headers_sent())
    {
        throw new Ay_Exception('Headers have been already sent. Cannot force file download.');
    }

    $file_name  = $file_name === NULL ? basename($file) : $file_name;

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $file_name);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

    exit;
}
著墨染雨君画夕 2024-12-26 22:23:04

在 php 方面,您真正需要的是将 Content-Disposition 标头设置为附件,如 header('Content-Disposition:attachment; filename=name-of-file') ;。如果您必须或想要在客户端执行此操作,浏览器将自动尝试下载大量文档类型,因此您只需提供指向它的直接链接即可。不过,显然这不会发生在你身上,所以它当然不可靠。

What you really need on the php side is to set the Content-Disposition header, to attachment, as in header('Content-Disposition: attachment; filename=name-of-file');. If you have to or want to do it on the client side, browsers will automatically attempt to download a lot of document types, so you can just provide a straight link to it. Apparently this is not happening for you, though, so of course it's not dependable.

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