Make sure you've ran php artisan storage:link on your production server, and check your permissions. Is the file really stored? Check the logs for some errors.
Nov 10, 2021
21
Level 1
After deployment of my project file download doesn't work
on my local machine file download function (ex:- download uploaded image, pdf) work smoothly. but when i published my project, click on download button it will redirect to logout. what is the reason?
controller
?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Course;
use App\Models\material;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Storage;
class AdminCourseMaterial extends Component
{
use WithFileUploads;
public $material = [];
public $month;
public $course;
public function render()
{
$files = material::paginate(10);
$courses = Course::all();
return view('livewire.admin-course-material',[
'courses'=>$courses,
'files'=>$files
]);
}
public function save()
{
$this->validate([
'material.*' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:10000',
'month'=>'required',
'course'=>'required'
]);
foreach($this->material as $pdf)
{
$data = new material();
$filename = $pdf->getClientOriginalName();
$pdf->storeAs('public',$filename);
$data->course_id = $this->course;
$data->file_name = $filename;
$data->month = $this->month;
$data->save();
}
}
}
view
<a href="{{ route('admin.file-download',$file->file_name) }}" class="btn bg-indigo"><i class="fas fa-cloud-download-alt mr-2"></i>Download</a>
route
Route::get('/course-material/download/{file_name}',[CourseMaterialsController::class,'download'])->name('file-download');
CourseMaterialsController
public function download($file_name)
{
$path = public_path('storage/'.$file_name);
return response()->download($path);
}
PreventBackHistory
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class PreventBackHistory
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$headers = [
'Cache-Control' => 'nocache, no-store, max-age=0, must-revalidate',
'Pragma','no-cache',
'Expires','Fri, 01 Jan 1990 00:00:00 GMT',
];
return $response;
}
}
Please or to participate in this conversation.