繁体   English   中英

当用户导航到另一个页面时自动开始下载

[英]Start download automatically when a user navigates to another page

我想知道如何实现我在几个网站上看到的效果,我点击下载现在链接,它带我到另一个页面,下载自动开始。 完成此任务的最佳方法是什么?

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

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

有关$filename的限制,请参阅此文章

编辑以响应andr的答案,相当于redirect-after-x-seconds的php将是:

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM