json(['message' => 'Still working.....'], 409); } $report = base64_decode($report); Cache::forget($hash); if($this->isXlsxData($report)){ return response($report, 200, [ 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Content-Disposition' => 'inline; filename="report.xlsx"', 'Content-Length' => strlen($report) ]); } // Check if the content starts with PDF signature (%PDF-) $isPdf = str_starts_with(trim($report), '%PDF-'); $attachment_name = $isPdf ? 'report.pdf' : 'report.csv'; $headers = [ 'Content-Disposition' => "attachment; filename=\"{$attachment_name}\"", 'Content-Type' => $isPdf ? 'application/pdf' : 'text/csv' ]; // Set appropriate filename extension return response()->streamDownload(function () use ($report) { echo $report; }, $attachment_name, $headers); } private function isXlsxData($fileData) { // Check minimum size (XLSX files are typically > 1KB) if (strlen($fileData) < 1024) { return false; } // Check ZIP signature $header = substr($fileData, 0, 4); if ($header !== 'PK' . chr(3) . chr(4)) { return false; } // Check for XLSX-specific content return strpos($fileData, '[Content_Types].xml') !== false; } }