1

Currently I am storing file into storage folder then show that file into browser and when close that file then remove that file from storage but I want directly show into browser and also can be download without store. This is working but storing file.

$fileFromAPI = $apiResponse->file;
$bin = base64_decode($fileFromAPI, true);
$path = config('pdf_paths.file') . $id . "/";
if (!is_dir($path)) {
    $oldmask = umask(0);
    Storage::makeDirectory($path, 0777, true);
    umask($oldmask);
}


Storage::put($path . 'file_' . $fid . '.pdf', $bin);
$pdfread = 'storage/' . $path . 'file_' . $fid . '.pdf';
return response()->file($pdfread)->deleteFileAfterSend(true);
Jsowa
  • 9,104
  • 5
  • 56
  • 60
rakshitjat
  • 146
  • 9
  • 1
    https://stackoverflow.com/a/36987211/1427878 – CBroe Dec 04 '20 at 12:52
  • its not show proper ... its look like this https://imgur.com/a/MsAvafq – rakshitjat Dec 04 '20 at 13:00
  • `$bin = base64_decode($fileFromAPI, true); header('Content-Type: application/pdf'); echo $bin;` – rakshitjat Dec 04 '20 at 13:00
  • That _is_ what the content of a PDF looks like, but your browser did not interpret it as a PDF - which means, your attempt at setting the header has likely failed. Do you have proper PHP error reporting enabled? – CBroe Dec 04 '20 at 13:12
  • i think header not included if i am remove header ... its give same output means its show base64_decode code – rakshitjat Dec 04 '20 at 13:21
  • i added base 64 code into this link...https://imgur.com/gxdKIJn when i am converting online its give proper output – rakshitjat Dec 04 '20 at 13:28
  • 1
    BTW thanks bro for give me your time ... i found solution based on your knowledge return response($bin) ->header('Content-Type', 'application/pdf'); – rakshitjat Dec 04 '20 at 13:41

1 Answers1

3

I found solution just set header and return it.

         $fileFromAPI = $apiResponse->file;
            $bin = base64_decode($fileFromAPI, true);

            return response($bin)
            ->header('Content-Type', 'application/pdf');
        
rakshitjat
  • 146
  • 9