Drupal 批处理 - 如何在完成回调中使用 StreamedResponse 进行重定向

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

我正在批量导出许多实体,然后下载结果文件。这是我完成回调的一部分:

    ....
    if ($success && !empty($results['file'])) {
      // Create a response to trigger the file download.
      $response = new StreamedResponse(function () use ($results) {
        readfile($results['file']);
        unlink($results['file']);
      });

      // Set response headers.
      $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      $response->headers->set('Content-Disposition', 'attachment; filename="exported_values_' . time() . '.xlsx"');
      $response->headers->set('Cache-Control', 'private');

      // Send the response.
      $response->send();
      ....

导出文件下载成功。

但是,我想随后重定向到特定页面,但不想停留在批处理页面上。

我尝试将

header('Location: xxurlxx')
添加到
StreamedResponse
函数中;然后也在外面使用
$response->headers->set('Location', xxurlxx);
但结果不好,因为它正确重定向但它不再下载文件。

请问有什么想法吗?

http drupal response httpresponse drupal-batch
1个回答
0
投票

当您从 php 发送文件时,您正在发送对请求的响应。这样请求就完成了。之后你就不能做任何事情了。

一种方法是用 Javascript 来完成,但这很棘手 除了首页上的 JavaScript 重定向之外,另一种方法是更改“下载文件”链接以在新选项卡中打开文件

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