1 year ago

Download a file from the database

Posted 1 year ago by jordano1

I have a FileController, a file.blade.php and 3 routes.

3 routes:

//this is running a method to store my files that I've uploaded
Route::post('/file-upload/', '[email protected]'); 

//this is returning the file-upload page with the files laid out in a foreachloop
Route::get('/file-upload/', '[email protected]'); 

//this route will fire when the user clicks to download a file on the file-upload page
Route::get('/file-upload/download/{file}', '[email protected]');

uploading a file will trigger this method:

public function store(Request $request)
        $files = Files::all();
        if($request->hasFile('file') && $request->file('file')->isValid()){
            //storing the input field with the name "file" in $file.
            $file = $request->file('file');
            //using laravel method to get filename from the inputfield, in case we add a feature to allow the user to provide the name
            $originalName = $file->getClientOriginalName();

            //storing file in public/upload saving as $originalName
            $fileLoc =  $request->file->storeAs('public/upload', $originalName);
            //getting mimetype from the file stored with the class storage using method mimetype of the file above
            $mimeType = Storage::mimeType($fileLoc);

            //creating a new file entry
            $entry = new Files();
            //storing mimetype of file
            $entry->mime = $mimeType;
            //storing filename of file
            $entry->original_filename =  $originalName;
            //storing name as ogname, but may change to allow users to name file
            $entry->name = $originalName;
            //saving and making entry to the DB

            //if statement checks if file is a file and is valid, otherwise no file to upload
            return "Failed to upload";

        return back(); compact('files');

When a user clicks on a file they uploaded:

public function download($file)
      $url = Storage::url($file);

        return Storage::download($url);
        // view("", compact('$download'));

I have confirmed via sequel pro that this works, the entry is recorded in the database, in the public/upload directory in public/storage/upload/(filesstoredhere), and displayed in the view.

the view file.blade.php (relevance to question, took out elements not applicable to issue):

<form method="POST" action="/file-upload/" >
    <input id="input-fa" name="file" type="file" class="validate" enctype="multipart/form-data">

<div class="col-sm">
    <a href="file-upload/download/{{$file->name}}" download="{{$file->name}}">
        <button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-download">download</i></button>

I'm not sure if I should be using in the href element the "download" property with the $file->name. This seems to download the file, but it will say that the file "filename.jpg" could not be opened because it is empty.

I tried using the download function but I just get the error:

(1/1) BadMethodCallException
Call to undefined method League\Flysystem\Filesystem::download

Any help would be appreciated, if you need more info let me know.


Please sign in or create an account to participate in this conversation.