简体   繁体   中英

Laravel: Slow download using Response::download

so I have a large (1.7mb) jpg file that I want to allow users to download as a screen wallpaper. I am using the following:

/**
* getWallpaper
* Download the wallpaper in jpg format from above web root
* 
* @return file
*/
public function getWallpaper()
{
$file = "../downloads/myfile.jpg";

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

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

This works really fast on my local dev server, but takes around 14 seconds for the browser download dialogue box to appear on my production server. Any ideas? Could be a setting miss-match at the server level, if so what settings should I be looking at? Thanks for any help. Ollie.

I'm just guessing here but you could try setting the attachment and content type header and then instantly call flush() to send that information to the client so it opens the dialog.

public function getWallpaper(){
    $file = "../downloads/myfile.jpg";

    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename="myfile.jpg"');

    flush();

    return Response::download($file, 'myfile.jpg');
}

In my opinion it feels a bit hacky (and redundant, since we're setting headers that are then again set in Response::download , but if it works you could maybe extend the Response class or something.

Just in case anybody is having the same issue - for me it turned out to be a network connection problem.

On my work connection, I still get a long delay before the download dialogue appears, whereas at home it appears almost instantly. I have no idea why though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM