dsykx64220 2017-06-02 09:49
浏览 386

如何通过ajax直接使用laravel excel触发excel下载

Our application send post parameters to the server via an ajax in order to generate data, format it into an excel, and then trigger a download of the created excel.

We did this previously with phpexcel, saving the excel in a tmp file, and then triggering a redirect to the route of the excel file. We are migrating our applications over to laravel but we are still doing this the same way, using the mmatwebsite laravel excel provider, saving a tmp file in the laravel public directory, and redirecting to that location. We want to download this directly without saving the in the tmp but only thing that every arrives is some garbled output in the network window.

How can this be done, and is this even possible, are we destined to use the tmp file download forver? Current simple excel function:`public function ExportCible(Request $request) {

  $cible = Cible::filter($request->all())->get();
  $columns = explode(",", $request->columns);
  $titles = explode(",", $request->titles);


  ob_end_clean();
  ob_start();
  $filename = "ciblage_d".date("Ymd",time())."_h".date("His",time());

  Excel::create($filename, function($excel) use($cible,$columns, $titles){

    $excel->sheet('Excel sheet', function($sheet)use($cible,$columns, $titles) {
      $sheet->setOrientation('landscape');
      $sheet->loadView('excelTemplates.cible')->with('cible', $cible)->with('columns', $columns)->with('titles', $titles);
    });

  })->store('xlsx', public_path('exports'));

  return($filename.".xlsx");

}`

And the call on the ajax side with the window location to trigger a download.

exportData:function(url,data){
return $.ajax(url,{
    data: data,
    method: "POST",
    success: function(fileLink){
        window.location = Config.exportRoute + fileLink
    },
});

},

What am I missing here. Tried the other laravel excel methods such as ->export() and ->download(). Tried similar methods in phpexcel previously. Never any luck

  • 写回答

1条回答 默认 最新

  • doublestar2014 2017-06-02 10:04
    关注

    If u insist using ajax,try return the excel file obj after created in your API,

    and try ajax method: "GET".

    Another way is set a temporary route for it and destory after operation finished, so what you can set cache or sth else to reduce the pressure of server.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题