【问题标题】:How do you conditionally export a CSV using ajax in laravel? [duplicate]如何在 laravel 中使用 ajax 有条件地导出 CSV? [复制]
【发布时间】:2019-12-20 22:07:01
【问题描述】:

我正在尝试使用 Laravel Excel 包 https://docs.laravel-excel.com 导出 CSV。 我需要在使用 ajax 请求时传递一些变量,这一切正常,但响应没有下载 CSV。

我在一些没有 ajax 的地方使用了这个包,只是一个简单的导出,它一切正常,所以我知道那一面很好。

这是我用来发送数据的 ajax:

$(document).ready(function() {
$( "#export" ).on( "click", function() {
                let clubInfo = $("#clubInfo select").val();
                let funderInfo = $("#funderInfo select").val();
                $.ajax({
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    type: 'post',
                    url: '{{route('export')}}',
                    data: {club : clubInfo, funder: funderInfo},
                }).done(function(response) {
                    console.log(response);
                }).fail(function() {
                    console.log("Failed");
                });
            });
});

这是处理导出的控制器:

public function adminExport(Request $request)
    {
        $club = $request->input('club');
        $funder = $request->input('funder');

        return Excel::download(new UsersExport($club, $funder), 'users.xlsx');
    }

我没有收到任何 PHP 错误或警告,这一切都很好,我只是在网络选项卡中收到了一个响应,看起来像是它只是没有下载的 csv。 响应的标题是:

Accept-Ranges: none
Cache-Control: public
Connection: keep-alive
Content-Disposition: attachment; filename=users.xlsx
Content-Length: 6812
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

【问题讨论】:

  • 这些是作为 ajax 响应的标头。您必须自行触发下载。如果响应中有 excel 表的二进制数据,那么您可以在 javascript 中创建一个对象 URL,将此 URL 设置为锚标记,然后单击它进行下载。
  • 在这种情况下不要使用 ajax 使用普通 php 简单提交表单,它会下载。

标签: php excel ajax laravel response


【解决方案1】:

我认为您需要告知出口的类型。请注意以下几点:

return (new InvoicesExport)->download('invoices.csv', \Maatwebsite\Excel\Excel::CSV);

但在你的控制器中,我看不到导出类型:

return Excel::download(new UsersExport($club, $funder), 'users.xlsx');

如果您使用的是 2.1 版,则需要这样做:

Excel::create('Filename', function($excel) {
   //
})->export('csv');

或者……你觉得先保存导出然后这样导出怎么样?

$file_name = 'my_export';

// Find users first
$users = User::where([['club', $club], ['funder', $funder]])->get();

// then generate the file
Excel::create($file_name, function ($excel) use ($users) {
    $excel->sheet('Users', function($sheet) use($users) {
        $sheet->fromArray($users);
    });
})->store('csv', storage_path('excel/export'));

// Finally, download the file
$full_path =  storage_path('excel/export') . '/' . $file_name . '.csv';

return Storage::disk('public')->download($full_path, $file_name . 'csv', 
    ['Content-Description' =>  'File Transfer','Content-Type' => 'application/octet- 
      stream','Content-Disposition' => 'attachment; filename=' . $file_name . 
      '.csv']);

或者你可能会这样做:

$headers = array(
    'Content-Type' => 'text/csv',
);

return Response::download($file_name . 'csv', $full_path, $headers);

【讨论】:

    【解决方案2】:

    除非通过 AJAX 获取文件是一个非常困难的要求,否则我建议使用window.open('your_url/your_route?club=XXXXX&funder=XXXXX', '_blank'); 来下载文件并避免任何麻烦。

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 2018-01-19
      • 1970-01-01
      • 2014-05-24
      • 2018-09-25
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多