表单提交后自动开始文件下载
我有一个用户在线填写的网络表单。当他们按下提交时,它将开始为他们下载文件。
目前,我处理表单提交并为用户生成合适的文件,并使用合适的标头将其启动。例如...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将表单发送到谢谢文档,并在文件下载中添加META刷新:
You could send the form to the Thank you document and put there a META refresh to the file download:
添加第二个页面,其中显示“谢谢,您的下载将在几秒钟内开始”,并使用 javascript:
或使用元重定向触发下载。
Add a second page that says something "thank you, your donwnload will start in a few seconds" and triggers the download using javascript:
or use a meta redirect.
您可以在页面中插入隐藏的 iframe 并将表单提交到此 iframe。
You can insert a hidden iframe into your page and submit your form to this iframe.