在webman中使用xlswriter导出excel,如何返回给前端二进制流?

longxiaowang

$file_name = time().'.xlsx';
$excel = new \Vtiful\Kernel\Excel(['path' => public_path().'/excel']);
$fileObject = $excel->constMemory($file_name);
$handle = $fileObject->getHandle();
$fileObject->data($excel_data);
$filePath = $fileObject->output();

    header("content-Type: application/vnd.openxmlformats-officedocument,spreadsheetml.sheet");
    header('Content-Disposition: attachment;filename="'.$file_name.'"'); 
    header('content-Length:'.filesize($filePath));
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate');
    header('Cache-Control: max-age=0');
    header('Pragma: public');
    if(ob_get_contents()){
        ob_clean();
    }  
    if(copy($filePath, 'php://output')=== false){
        throw new Exception($filePath ."地址出问题了");
    }else{
        return $this->json(200, '', ['list'=>$filePath, ]); 
    }
    flush();     
    以上显示二进制流打印在启动界面,怎么在浏览器中返回给前端呢?求专家指点。
155 2 0
2个回答

ichynul
return response()->download($filePath, $file_name);
  • longxiaowang 1天前

    学习,感谢。根据教程实现后端返回文件二进制格式,但前端提示跨域问题,后端这里如何解决?

  • ichynul 21小时前

    直接下载文件应该不会吧,除非你在浏览器里面解析excel显示

damao
  • longxiaowang 1天前

    学习,感谢。根据教程实现后端返回文件二进制格式,但前端提示跨域问题,后端这里如何解决?

  • tanhongbin 1天前

    有一个跨域中间件直接用上即可

🔝