如何在Gravity Forms AJAX提交后启动文件下载

时间:2018-03-15 12:04:57

标签: php jquery ajax wordpress gravity-forms-plugin

我有一个Gravity Form,用户可以设置复选框来选择要下载的文件。对于增强的UX,表单是多页的并使用AJAX。

使用以下PHP函数和Gravity Forms Hook,我可以遍历所选的复选框并将文件添加到ZIP存档中,但目前无法开始下载。

add_filter( 'gform_after_submission_3', 'download_brochures_message', 10, 4 );
function download_brochures_message( $entry ) {

$confirmation = "Your download should begin shortly. \r\n";
$error = "Files not set \r\n";

$file1 = rgar( $entry, '1.1' );
$file2 = rgar( $entry, '1.2' );
$file3 = rgar( $entry, '1.3' );
$file4 = rgar( $entry, '1.4' );

$files = array(
        $file1,
        $file2,
        $file3,
        $file4
    );

print_r($files);

$zip = new ZipArchive();

$current_time = time();

$file_folder = wp_upload_dir();

$dir = $file_folder['path'];

$zip_file = tempnam( $dir, time() );

$zip->open( $zip_file, ZipArchive::CREATE );

foreach ($files as $file) {
    $zip->addFile($file);
}

$zip->close();

echo $zip_file;

header('Content-disposition: attachment; filename="'.$zip_name.'"');
header('Content-type: application/zip');
header('Content-Length: ' . filesize($zip_file));
header("Location: $zip_file");
readfile($zip_file);
}

由于表单通过AJAX提交,页面标题已经发送,我收到的PHP错误是#34;无法修改标题信息 - 标题已经发送"。如何修改此代码以允许下载zip?

0 个答案:

没有答案