本文介绍了表单提交后自动启动文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



此刻,我处理表单提交,并为用户生成一个合适的文件,并将其从合适的标题例如...

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

但是,由于这开始立即下载,原来的表单将被浏览器留在屏幕上。



一旦下载完成(或下载开始之前),我真的想去一些谢谢屏幕。我知道这是可能的,因为你访问的几乎每个下载网站都会这样做(通常会在下载开始之前充满广告)。



那么,如何显示一个谢谢屏幕,一秒钟后开始下载?



如何解决方案会影响后退按钮的行为,因为我不想要文件再次下载,而不填写表单?



我在服务器上使用PHP,可以依靠客户端上可用的JavaScript(和jQuery)。



谢谢。

解决方案

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

 < meta http-equiv =refreshcontent =3; url = download.php> 
< p>谢谢!下载将在3秒内开始。如果没有,请使用此链接下载< a href =download.php>文件< / a>< / p>


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.

解决方案

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>

这篇关于表单提交后自动启动文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 23:20