【问题标题】:Start download automatically when a user navigates to another page当用户导航到另一个页面时自动开始下载
【发布时间】:2010-07-23 17:18:24
【问题描述】:

我想知道如何实现我在几个网站上看到的效果,我单击立即下载链接,它会将我带到另一个页面,然后自动开始下载。实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: php javascript download


    【解决方案1】:

    重定向到发出以下标题的页面:

    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Length: $length");
    

    有关$filename 的限制,请参阅this post

    edit 响应 andr 的回答,redirect-after-x-seconds 的 php 等效项是:

    header("Refresh: 2; url=start_download.php");
    

    (虽然你应该正式指定一个完整的 URL,我认为)start_download.php 将包含上面的两行。

    【讨论】:

      【解决方案2】:

      首先显示包含一些内容的页面(请稍候,等等),然后重定向到文件本身或输出文件的脚本。 重定向是通过元标记或 javascript 完成的:

      1. html:<meta http-equiv="refresh" content="5;url=http://example.com/foo.zip" /> 其中 «5» 以秒为单位
      2. 页面加载时的 js:setTimeout("location.href=http://example.com/foo.zip", 5000) 其中 «5000» 以毫秒为单位。

      如果您选择通过 php 脚本输出文件,请按照 mvds 的回答。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-03
        • 2010-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-12
        相关资源
        最近更新 更多