samedi 3 octobre 2015

Files downloaded will only show up in browser - Laravel 5

I'm absolutely stumped over this. Currently whenever I attempt to download a file through the Response::download() method I'll see the file itself popup on my browser, but it won't actually be "downloaded" permanently to my disk. It's similar to when you click on "view image" and have that pop up in your full browser screen. I want my files to be sent to my dedicated /downloads/ folder on my computer that files usually go it when you click a download link.

I've been banging my head against this, If anyone has a solution to this I will be eternally grateful.

Routes

Route::get('/assets/{name}/{file_type}', 'FilelistsController@downloadFileOnShowPage');

Download Link

<h3><a href="/assets/{{ $filelist->name }}.{{ $filelist->file_type }}">Download File</a></h3>

downloadFileOnShowPage Function in FilelistsController

public function downloadFileOnShowPage($name, $file_type){
    $downloadPath= public_path()."/assets/" . $name;
    $headers = array(
    'Content-Type: application/octet-stream',
    'Content-Type: application: application/pdf',
);
    return Response::download($downloadPath, $name . '.' . $file_type, $headers);

}

File Upload Function (thought I'd provide it if this helps the context)

public function store()
{
    $filelist = new Filelist($this->request->all());
    //store file details - thanks to Jason Day on the unit forum for helping with some of this
    $filelist->name = $this->request->file('asset')->getClientOriginalName();
    //see http://ift.tt/1WEMuyd
    $filelist->name = $withoutExt = preg_replace('/\\.[^.\\s]{2,4}$/', '', $filelist->name);
    $filelist->file_type = $this->request->file('asset')->getClientOriginalExtension();
    $filelist->file_size = filesize($this->request->file('asset'));
    $filelist->uploader_name = Auth::user()->email;
    $filelist->save();
    // Save uploaded file
    if ($this->request->hasFile('asset') && $this->request->file('asset')->isValid()) {
        $destinationPath = public_path() . '/assets/';
        $fileName = $filelist->name . '.' . $this->request->file('asset')->guessClientExtension();
        $this->request->file('asset')->move($destinationPath, $fileName);

    }

    $fullfilepath = $destinationPath . $filelist->name;
    $filelist->file_path = $fullfilepath;
    $filelist->save();



    return redirect('filelists');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire