简体   繁体   English

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

[英]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完成:

  1. html: <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»是秒
  2. js on page load: 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.

相关问题 用户刷新页面或导航到其他页面时,侧边栏上的其他广告 - Different ad on sidebar when user refreshes page or navigates to different page 单击“提交”按钮导航到另一页 - Clicking on 'submit' button navigates to another page 当用户离开页面时,通过Ajax调用php函数 - Call php function via ajax when user navigates away from page 当用户离开页面时,如何重置会话? - How can I reset session when user navigates away from page? 我为我的域设置了一个全局cookie,当用户导航到一个页面时,我想检查该cookie是否存在; 如果没有那么它会给他们404 - ive set a global cookie for my domain and when user navigates to a page i want to check if that cookie exists; if not then it will give them a 404 当用户从站点 A 导航到站点 B 时,我们如何将 woocommerce 购物车详细信息从一个站点发送到另一个站点 - How we can send woocommerce cart details from one website to another when user navigates from site A to site B 当用户离开数据库的“购物车”页面时,如何保存购物车 - How can I keep a shopping cart populated when the user navigates away from the 'cart' page, wihtout using a database 自动开始读取另一个用户的会话 -php,apache - Automatically start reading another user's session -php,apache 如何在 PHP 中自动开始下载? - How to Automatically Start a Download in PHP? 开始自动下载PDF文件 - Start download PDF file automatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM