简体   繁体   English

HTML表单转到新页面并开始下载

[英]HTML form to go to new page and start download

I have an HTML form like so: 我有一个像这样的HTML表单:

<form method="post" enctype="multipart/form-data" action="script.php" align="center">
    <input type="submit" value="Run Program"/>
</form>

Then I have the PHP page, script.php. 然后我有PHP页面script.php。 I would it to be displayed and also begin a download automatically. 我会显示它并自动开始下载。 However, when I add the header for starting the download: 但是,当我添加标题以开始下载时:

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=' . $_FILES['upload']['name'] . '.tbx');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($tbx));

the download is started but the page itself is not loaded (probably because the headers say that we are just going to transfer a file). 下载已启动,但页面本身未加载(可能是因为标题说我们只是要传输文件)。 How do I make the page properly display and automatically start a download? 如何正确显示页面自动开始下载?

You can't do that server-side. 你无法做到服务器端。 You have to do that client-side. 你必须做那个客户端。

You can only send one resource at a time over HTTP! 您一次只能通过HTTP发送一个资源! Just send a page that includes a JavaScript redirect to your download script. 只需将包含JavaScript重定向的页面发送到您的下载脚本即可。

You need to load the HTML page first, then at the end of the page perform a JavaScript location request to the download file. 您需要先加载HTML页面,然后在页面末尾对下载文件执行JavaScript位置请求。

<html>
... your HTML ....
<script type="text/javascript">
document.location = '... location of file to download ...';
</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM