我有一个提交到php脚本的表单,我想执行以下操作:
编辑:仅供参考,我通过jquery调用php:
$.ajax({
type: "POST",
url: php_url,
data: $('#popForm').serialize(),
success: function(){
window.location.href = 'downloadpdf.php?file=test.pdf';
}
})
这是通过POST捕获用户输入并通过电子邮件发送给我的php代码。我只需要一个上面#3的部分。
<?php
$email_PGi = "me@mail.com";
$email_subject = "some email subject";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email_message = "The following is a new message received via the website:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($firstname)."\n";
$email_message .= "Last Name: ".clean_string($lastname)."\n";
// create email headers
$headers = 'From: '.$biz_email."\r\n".
'Reply-To: '.$biz_email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_PGi, $email_subject, $email_message, $headers);
?>
downloadpdf.php
<?php
$file = $_GET['file'];
header('Content-Type: Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . $file);
readfile($filename);
die();
?>
答案 0 :(得分:1)
这是因为ajax请求正在接收响应(具有正确的标头),该请求不会输出给用户。您可以尝试以下三种方法之一:
window.location
值设置为仅使用Content-Disposition: attachment
标头的脚本,以便浏览器开始“重定向”,而是按标题所示下载文件。答案 1 :(得分:0)
使用http://php.net/manual/en/function.file-get-contents.php将数据与标题一起发送,而不是readfile。
答案 2 :(得分:0)
使用:
define('PDF_FILE', 'test.pdf');
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=" . basename(PDF_FILE));
header('Expires: 0');
header('Content-Length: ' . filesize(PDF_FILE));
ob_clean();
flush();
readfile(PDF_FILE);
答案 3 :(得分:0)
您是否尝试过打开强制下载的窗口?这可能不是最好也不是最干净的解决方案,但它会起作用。
JQuery的:
$.ajax({
type: "POST",
url: php_url,
data: $('#popForm').serialize(),
success: function()
{
window.location.href = 'downloadpdf.php?file=test.pdf';
}
});
downloadpdf.php?文件=检验.pdf
$file = $_GET['file'];
header('Content-Type: Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . $file);
readfile($filename);
die();
我不确定你是否打算稍后将文件名传递给回调。