简体   繁体   English

下载文件 laravel 5.7 后重定向

[英]Redirect after download file laravel 5.7

Hello there你好呀

Hope you will be doing well.I want to redirect to a route after file download but as we know that return only works once in a controller method how i can achieve this with laravel 5.7.I have to set a session and display it when data exported in txt file.I want this with post method.希望你会做得很好。我想在文件下载后重定向到一个路由,但正如我们所知,返回只在控制器方法中有效一次,我如何用 laravel 5.7 实现这一点。我必须设置一个会话并在数据时显示它导出到 txt 文件中。我想要使用 post 方法。 Every thing is fine but redirect is not working;一切都很好,但重定向不起作用;

Controller Method控制器方法

public function exportTxtProcess(Request $request)
{
    $table = $request->tblExportSelect;

    $destinationPath = public_path('/');

    $result;

    $outputs = DB::select("SELECT * FROM $table");

    $today = date("Y-m-d");
    $fileName = $table . "-" . $today;
    $fp = fopen($destinationPath . "$fileName.txt", "wb");

    foreach ($outputs as $output) {
        $output = (array)$output;

        @array_shift($output);

        $removeUserId = @$output['user_id'];
        $created_at = @$output['created_at'];
        $updated_at = @$output['updated_at'];

        if (($key = array_search($removeUserId, $output)) !== false) {
            unset($output[$key]);
        }
        if (($key1 = array_search($created_at, $output))) {

            unset($output[$key1]);
        }

        if (($key2 = array_search($updated_at, $output))) {

            unset($output[$key2]);
        }

        if (is_null($created_at) OR $created_at == '') {
            unset($output['created_at']);
        }

        if (is_null($updated_at) OR $updated_at == '') {
            unset($output['updated_at']);
        }


        $netResult = $this->getTableFields($table, $output);

        fwrite($fp, $netResult);

    }

    $result = fclose($fp);


    if ($result) {
        $pathToFile = $destinationPath . "$fileName.txt";

        $redirect = redirect()->back();
        $sess = Session::flash('success', 'Table exported successfully');
        return response()->download($pathToFile)->deleteFileAfterSend(true);


    }

}

Thank in advance预先感谢

You can only have 1 response, so it's impossible to instruct a double return.您只能有 1 个响应,因此不可能指示双重返回。 What you could do, is set the filename you wish to have downloaded in a session variable, then redirect back to whatever page.您可以做的是在会话变量中设置您希望下载的文件名,然后重定向回任何页面。 Within the redirected page, you could flash your message, along with having an automatic download of the file.在重定向的页面中,您可以闪现您的消息,同时自动下载文件。

Here is some threads on the topic:以下是有关该主题的一些主题:

How do I redirect after download in Laravel? 在 Laravel 中下载后如何重定向?

PHP generate file for download then redirect PHP生成文件下载然后重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM