-1

I have a base64 encoded image which I want to send to my vue frontend for download. It seems that this is how it's done for images in your filesystem:

return response()->download($filePath);

but how would I do the same for a base64 encoded image without having to store it in the filesystem? Something like:

return response()->download($base64Image);

shows that the file doesn't exist.

Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132

1 Answers1

0

You should send base64 content as string in http response not as attachment and take care to transform it from vuejs controller, example below:

// php
return response()->make($base64Image);
// js -> https://stackoverflow.com/questions/14011021/how-to-download-a-base64-encoded-image
const img = api.getImage(); // base64 content
<a download="FILENAME.EXT" href="`data:image/png;base64,${img}`">Download</a>

download() method from response() helper expect \SplFileInfo or string of file path as first argument, so it can't work in your case.

    /**
     * Create a new file download response.
     *
     * @param  \SplFileInfo|string  $file
     * @param  string|null  $name
     * @param  array  $headers
     * @param  string|null  $disposition
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download($file, $name = null, array $headers = [], $disposition = 'attachment');
Lounis
  • 597
  • 7
  • 15