表单提交后自动开始文件下载

发布于 2024-08-06 08:32:50 字数 574 浏览 1 评论 0原文

我有一个用户在线填写的网络表单。当他们按下提交时,它将开始为他们下载文件。

目前,我处理表单提交并为用户生成合适的文件,并使用合适的标头将其启动。例如...

header('Content-type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourfile.txt"');
header("Content-Transfer-Encoding: binary");

但是,由于这会立即开始下载,因此浏览器会将原始表单保留在屏幕上。

下载完成后(或在下载开始之前),我真的很想进入一些“谢谢”屏幕。我知道这是可能的,因为几乎您访问的每个下载网站都会这样做(通常会在下载开始之前向您推送大量广告)。

那么,如何显示“谢谢”屏幕并在一秒钟后开始下载?

所提出的任何解决方案将如何影响后退按钮的行为,因为我不希望在不重新填写表单的情况下再次下载文件?

我在服务器上使用 PHP,并且可以依赖客户端上可用的 Javascript(和 jQuery)。

谢谢。

I have a web form that users complete online. When they press submit it will start a file download for them.

At the moment, I process the form submission and generate a suitable file for the user and fire it off with suitable headers. eg...

header('Content-type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourfile.txt"');
header("Content-Transfer-Encoding: binary");

However, since this starts a download right away, the original form is left on screen by the browser.

I would really like to go to some "Thank you" screen once the download completes (or before the download starts). I know it is possible, because almost every download site you visit does this (normally to pump you full of adverts before the download starts).

So, How do I show a "Thank You" screen that starts the download after a second?

How would any solution proposed effect the behaviour of the back button, as I don't want the file downloading again without the form being refilled?

I am using PHP on the server and can rely on Javascript (and jQuery) being available on the client.

Thank you.

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

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

发布评论

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

评论(3

浅忆 2024-08-13 08:32:50

您可以将表单发送到谢谢文档,并在文件下载中添加META刷新

<meta http-equiv="refresh" content="3;url=download.php">
<p>Thank you! The download will start in 3 seconds. If not, use this link to download the <a href="download.php">file</a></p>

You could send the form to the Thank you document and put there a META refresh to the file download:

<meta http-equiv="refresh" content="3;url=download.php">
<p>Thank you! The download will start in 3 seconds. If not, use this link to download the <a href="download.php">file</a></p>
半边脸i 2024-08-13 08:32:50

添加第二个页面,其中显示“谢谢,您的下载将在几秒钟内开始”,并使用 javascript:

$(document).ready(function(){
  window.setTimeout(function(){
    window.location = 'http://yourdownloadhost.com/file.zip';
  }, 1500);
});

或使用元重定向触发下载。

Add a second page that says something "thank you, your donwnload will start in a few seconds" and triggers the download using javascript:

$(document).ready(function(){
  window.setTimeout(function(){
    window.location = 'http://yourdownloadhost.com/file.zip';
  }, 1500);
});

or use a meta redirect.

北凤男飞 2024-08-13 08:32:50

您可以在页面中插入隐藏的 iframe 并将表单提交到此 iframe。

You can insert a hidden iframe into your page and submit your form to this iframe.

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