如何完成批处理,然后重定向

问题描述 投票:1回答:2

我正在研究Drupal 7项目。我正在使用批处理,需要进行一些操作,并显示进度条。在我的“ formulaire_finished”中,该操作最后执行,作为批处理的最后一项操作,我想下载一个文件,然后重定向到另一个页面,因为该过程已结束。

但是,readfile()函数使用了drupal_exit(),因此我的重定向未完成。

这是我的代码:

function formulaire_finished($success, $results, $operations) {
    if ($success) {
        $path = $results['infos']['path'];
        download_file($path);
        // Redirection
        drupal_goto($_SESSION['my_page'], array('query' => array('details' => '1')));
    } else {
        // An error occurred.
        // $operations contains the operations that remained unprocessed.
        $error_operation = reset($operations);
        drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE), )));
    }
}

下载功能:

function download_file($path) {
    $dir = $path;
    $filename = basename($dir);
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
        drupal_add_http_header('Pragma', 'public');
        drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
    } else {
        drupal_add_http_header('Pragma', 'no-cache');
    }
    drupal_add_http_header('Expires', '0');
    drupal_add_http_header('Content-Type', 'application/vnd.ms-excel');
    drupal_add_http_header('Content-Disposition', 'attachment; filename=' . basename($dir) . ';');
    drupal_add_http_header('Content-Transfer-Encoding', 'binary');
    drupal_add_http_header('Content-Length', filesize($dir));
    readfile($dir);
    unlink($dir);
    drupal_exit();
}

欢迎所有想法。

redirect drupal download content-management-system readfile
2个回答
0
投票

我认为您不应该在批处理操作的完成回调期间输出要下载的文件。您应该将文件保存到drupal的文件系统中,并保存对该文件的引用。批处理完成后,您可以下载它。


0
投票

在下面的函数中,您可以尝试始终从下面的函数返回true吗?

function download_file($path) {
    $dir = $path;
    $filename = basename($dir);
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
        drupal_add_http_header('Pragma', 'public');
        drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
    } else {
        drupal_add_http_header('Pragma', 'no-cache');
    }
    drupal_add_http_header('Expires', '0');
    drupal_add_http_header('Content-Type', 'application/vnd.ms-excel');
    drupal_add_http_header('Content-Disposition', 'attachment; filename=' . basename($dir) . ';');
    drupal_add_http_header('Content-Transfer-Encoding', 'binary');
    drupal_add_http_header('Content-Length', filesize($dir));
    readfile($dir);
    unlink($dir);

    //drupal_exit();
    // Can you use simple return here
    return true;

}

0
投票

此链接可能是一个选项https://www.jeffgeerling.com/blogs/jeff-geerling/using-batch-api-build-huge-csv

描述的方法不是那么简单,但是肯定可以。

© www.soinside.com 2019 - 2024. All rights reserved.