Laravel:I can’t Download a file

Asked

Viewed 42 times

1

I have tried several tutorials and researched similar problems here in stack,but without success ,previously I took the id of the file and it was passed as parameter and did not return anything.Now finally I can locate the file by name however the download not performed,it downloads an html and gives error on the server, besides which it does not take the format of the file.


Routes

Route::get('/download/{fileId}', 'ArquivosController@download');

Controller

<?php

namespace App\Http\Controllers;

use App\Arquivos;
use FontLib\Table\Type\name;
use Illuminate\Http\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;


class ArquivosController extends Controller
{
    public function index()
    {
        return view('templates')->with('arquivos', Arquivos::get());
    }
//Adicionar
    public function store(Request $request)
    {
        $this->validate($request, [
            'nome' => 'required',

        ]);

        $arquivo = new Arquivos;
        if (is_null($request->input('nome'))) {
            $arquivo->nome = str_replace('.' . $request->arquivo->extension(), '', $request->arquivo->getClientOriginalName());
        } else {
            $arquivo->nome = $request->input('nome');
        }
        $arquivo->tamanho = $request->arquivo->getClientSize();
        $arquivo->tipo = $request->arquivo->extension();
        $arquivo->caminho = 'files/' . $request->arquivo->storeAs('', str_slug($arquivo->nome) . '.' . $arquivo->tipo, 'upl_arquivos');
        $arquivo->save();
        return back()->with("Upload do arquivo '{$arquivo->nome}' realizado com sucesso.");
    }

    //Excluir
    public function destroy(Request $request) {
       Arquivos::destroy($request->id);
        return redirect('/templates');
    }

    //Download
    public function download()
    {
        $downloads = Arquivos::saved('TEMPLATE')->get();
        return view('/templates',compact('downloads'));



    }





}

Index

  <div class="panel-footer" style="padding:0px;">
                        <table class="table">
                            <tr>
                                <th>Nome</th>
                                <th>Tipo</th>
                                <th></th>
                            </tr>
                            @foreach($arquivos as $arquivo)
                                <tr>
                                    <td>{{$arquivo->nome}}</td>
                                    <td>{{$arquivo->tipo}}</td>


                                    <td>  <a href="download/{{$arquivo->nome}}" download="{{$arquivo->nome}}" >Download</a></td>
                                    <td><a href = 'delete/{{ $arquivo->id }}' >Delete</a></td>

                                </tr>
                            @endforeach
                        </table>
                    </div>

I can Upload and delete normally,I cannot identify the error in downlaod, as I am still a beginner in the framework(v.5.3).

  • The method is wrong because there should be one for each operation.

  • @Virgilio Novic didn’t understand what you meant,

  • One you show the view to the other you run the download commands and you made a method for everything...

  • You in the path ask fileid and the function does not have the variable with the same name

1 answer

0

Try something like

public function getDownload()
{

    $file= public_path(). "/download/info.pdf";

    $headers = array(
              'Content-Type: application/pdf',
            );

    return Response::download($file, 'filename.pdf', $headers);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.