EddiePuig's avatar

How to automatically download a file of storage???

I have a file in my storage folder, how can i download that file automatically?? With just a click!!??

0 likes
9 replies
sumbria's avatar
return response()->download($file, 'filename.pdf', ['Content-Type' => 'application/pdf']);
EddiePuig's avatar

@realrandyallen look, inside my function i have:

Storage::put('a.cfg', $some->text);

    return Storage::download('a.cfg'); 

Then in the browser inspector, when i go to the Network tab i get the response, i double-click it and then download the file. I dont know what to do!!!

EddiePuig's avatar

@MICHAELNGUYEN547 - Yes, im begginer. Im trying to send some info to put in a file and download the file, with only one click of a button. Do you have any better way?

amk's avatar

In Controller

use Storage;

public function fordownload(Request $request){

Storage::put('your_file_name', $request->info);

return Storage::download('your_file_name'); 

}

In Route

Route::post('download','YourController@fordownload')->name('down');

In Blade

            <form action="{{route('down')}}" method="post">
                {{ csrf_field() }}
                <div class="form-group">
                    <label for="info">Add Info</label>
                    <input type="text" name="info">
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>

you want like that?

EddiePuig's avatar

@AMK - I have tried this, but im getting 419 error, unknown status

amk's avatar

Try this bro.... I have been test it. It generate myfile.txt in public folder. pls check public folder.

public function fordownload(Request $request){
 $fileName= "myfile.txt";
 \File::put(public_path($fileName),$request->info);
 return response()->download($filename);
}

Please or to participate in this conversation.