[英]Start download automatically when a user navigates to another page
I was wondering how to accomplish an effect I've seen on several websites, where I click a download now link, it takes me to another page, and the download starts automatically. 我想知道如何实现我在几个网站上看到的效果,我点击下载现在链接,它带我到另一个页面,下载自动开始。 What is the best way to accomplish this? 完成此任务的最佳方法是什么?
Redirect to a page which emits the following headers: 重定向到发出以下标头的页面:
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $length");
See this post about the restrictions on $filename
. 有关$filename
的限制,请参阅此文章 。
edit in response to andr's answer, the php equivalent of redirect-after-x-seconds would be: 编辑以响应andr的答案,相当于redirect-after-x-seconds的php将是:
header("Refresh: 2; url=start_download.php");
(although you should officially specify a complete URL, I think) where start_download.php would contain the two lines above. (虽然你应该正式指定一个完整的URL,我认为)start_download.php将包含上面的两行。
First you show the page with some content (please wait, blah blah) and then redirect to the file itself or to the script which outputs the file. 首先,您显示包含一些内容的页面(请等待,等等),然后重定向到文件本身或输出文件的脚本。 The redirect is done either via meta tag or javascript: 重定向通过元标记或javascript完成:
<meta http-equiv="refresh" content="5;url=http://example.com/foo.zip" />
where «5» is in seconds html: <meta http-equiv="refresh" content="5;url=http://example.com/foo.zip" />
其中«5»是秒 setTimeout("location.href=http://example.com/foo.zip", 5000)
where «5000» is in milliseconds. js on page load: setTimeout("location.href=http://example.com/foo.zip", 5000)
其中«5000»以毫秒为单位。 If you choose to output the file via php script, follow mvds's answer. 如果您选择通过php脚本输出文件,请按照mvds的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.