[英]Get content of a request with 'content-disposition: attachment' header in Php
[英]PHP Header : Content-disposition: attachment , then Location: some url
我的目标是在浏览器发出附件下载后将浏览器重定向到新页面。
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header("Content-disposition: attachment;filename=file.csv");
// csv output logic here
// forward to next page
header("Location: $url");
可能吗 ?
更新 :上述结果
CSV的最后一行显示PHP错误:
无法修改标头信息 - 已由...发送的标头
这是因为CSV逻辑部分修改了行header("Location: $url");
之前的内容header("Location: $url");
更新 :我尝试了另一种方法:回显一个由关键行组成的小HTML <meta http-equiv="refresh" content="0; url=$url" />
(当然<html>
<head>
<body>
甚至DOCTYPE也得到了回应)。 但是,HTML代码仍显示在CSV内容中。
使用当前代码,没有你不能。 您强制浏览器将更多内容下载到file.csv,以便忽略更新的标头。
但是,看看元刷新的东西。 您将能够在页面加载之间添加延迟(以便用户实际看到网页)并在刷新时将文件发送到下载。
我希望我可以添加一个例子,但我现在正坐在一辆被一些婴儿哭泣的火车里,我很无聊。
为防止元标记(和其他HTML)出现在CSV文件内容中,我们可以使用一些$ _GET值。
<?php
if (isset($_GET['dl']) && $_GET['dl'] == '1') {
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header("Content-disposition: attachment;filename=file.csv");
// csv output logic here
}
else{
?>
web page here, with html, head, title, body, and whatnot tags.
In the head tag, add this:
<meta http-equiv="refresh" content="5;url=http://my.web.site.com/download.php?dl=1">
<?php
}
?>
您可以在客户端执行此操作:在新窗口中打开文件下载,然后使用javascript重定向当前窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.