简体   繁体   中英

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在此处输入图像描述

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');
    }

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

I hava the issue. Looking in inertia I was thinking to use inertia::location() to redirect for a page no inertia. But I don't if will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM