zakzouk
zakzouk

Reputation: 135

Laravel & inertia.js file download

I uploaded a file to the database and created Storage link using "php artisan storage:link" and everything work perfectly but when I want to download the file I face this error enter image description here

here's my code

route

Route::get('/download/{id}', [\App\Http\Controllers\SubjectController::class, 'downloadFile'])->name('subject.download');

Vue.js

<inertia-link :href="'/download/'+slide.id" class="rounded-lg bg-gray-200 px-4 py-1">
      <Icon name="download"></Icon>
</inertia-link>

the Controller

    public function downloadFile($id) {
        $file = Upload::find($id);
        $subjectCode = $file->subject()->get()->map->only('name','code')->first()['code'];
        $subjectName = $file->subject()->get()->map->only('name','code')->first()['name'];
        return response()->download(storage_path('app/public/documents/'.Str::upper($subjectCode.'_'.$subjectName).'/'.$file->file), 'public');
    }

Upvotes: 8

Views: 14073

Answers (1)

Kamlesh Paul
Kamlesh Paul

Reputation: 12401

replace

<inertia-link :href="'/download/'+slide.id" class="rounded-lg bg-gray-200 px-4 py-1">
                                    <Icon name="download"></Icon>
                                </inertia-link>

to

<a :href="'/download/' + slide.id" class="rounded-lg bg-gray-200 px-4 py-1">
    <Icon name="download"></Icon>
</a>

then it will not handle by inertia and it will let you download file

Upvotes: 24

Related Questions