0

OutputWhen I run the code, the image I want to download from my folder appears in XHR -> preview in Chrome but doesn't want to start download. I use the code according to the Laravel doc:

$file = public_path()."/files/projects/".$name;
$headers = array('Content-Type: image/jpeg');
return response()->download($file,$name,$headers);

I don't see what is wrong. It's pretty similar to many code I have seen.

  • what is the error message? – sumit Feb 27 '19 at 23:31
  • There is no error it's just doesn't want to download. It appears in XHR -> preview in chrome only @sumit. – Hendy Saint-Jusna Feb 27 '19 at 23:34
  • Just reading [the Laravel docs](https://laravel.com/docs/5.7/responses#file-downloads), I see `... requires the file being downloaded to have an ASCII file name` - could that be the problem? – Don't Panic Feb 27 '19 at 23:48
  • Could you include the html of your page and explain the user actions required for downloading the image? This might not be a problem with your Laravel code – Travis Britz Feb 28 '19 at 00:46

1 Answers1

0

We use headers and we have Laravel 5.1 but we do something a little different where we cache our files for a short time and create dynamic images of graphs using c3 plugin. At the end of my post I will show how we use these headers w/cache.

Make sure you add a route:

Route::get('/files/projects/{file}', 'DownloadController@index');

See this SO post

$file= public_path(). $name;

Laravel < 5

$headers = array(
          'Content-Type: image/jpeg',
        );

  return Response::download($file, $name.'jpg', $headers);

Laravel >= 5

  return response()->download($file, $name."jpg");

NOTE: Here is a cached image implementation ...(notice the base64_encode for binary files ie: .png .jpg)

  $responseHeaders = array(
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
            'Content-Type' => 'image/png',
            'Content-Disposition' => 'attachment; filename=' . $outputFileName . '.png',
            'Expires' => '0',
            'Pragma' => 'public'
        );


$url = action('DownloadController@standAlone', ['cat' => $category]);

        // Values cached for 1 minute... ???
        if (Cache::has($url)){
            return Response(base64_decode(Cache::get($url)))->withHeaders($responseHeaders);
        } else {
            $maxWidth = 2560;
            $maxHeight = 854;
            $minWidth = 780;
            $minHeight = 260;

            if ($height > $maxHeight) {
                $height = $maxHeight;
            }

            if ($height < $minHeight) {
                $height = $minHeight;
            }

            if ($width > $maxWidth) {
                $width = $maxWidth;
            }

            if ($width < $minWidth) {
                $width = $minWidth;
            }

            $command = app_path('bin/phantomjs');
            $command .= ' ' . app_path('bin/maketrendpng.js');
            $command .= ' ' . escapeshellarg($url);
            $command .= ' /dev/stdout';
            $command .= ' ' . escapeshellarg($height);
            $command .= ' ' . escapeshellarg($width);
            $command .= ' ' . request()->server->get('SERVER_NAME');
            $command .= ' ' . escapeshellarg($_COOKIE[env('SESSION_COOKIE', 'laravel_session')]);
            $command .= ' ' . escapeshellarg(env('SESSION_COOKIE', 'laravel_session'));

            $handle = popen($command, 'r');

            $contents = '';

            while (!feof($handle)) {
                $contents .= fread($handle, 8192);
            }

            pclose($handle);

    Cache::put($url, base64_encode($contents), 1);

    return Response($contents)->withHeaders($responseHeaders);
yardpenalty.com
  • 1,244
  • 2
  • 17
  • 32