DcatAdmin 中,调用Grid 类的 tools() 方法增加一个添加自定义按钮,实现下载模板文件,应该如何写路径

Laravel 版本为5.8
Dcat Admin 版本为2.2.2-beta
在控制器里,新增了一个按钮

            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(MarketExpenseTemplate::make());
            });

在Actions里增加了一个Actions,

<?php
namespace App\Admin\Actions;

use Dcat\Admin\Actions\Action;
use Dcat\Admin\Actions\Response;
class MarketExpenseTemplate extends Action
{
    /**
     * @return string
     */
    protected $title = '<button class="btn btn-outline-info"><i class="feather icon-download"></i>下载导入模板</button>';

    /**
     * Handle the action request.
     *
     * @return Response
     */
    public function handle()
    {
        $file = 'storage/模板.xlsx';
        return $this->response()->download($file);
    }
}

php artisan storage:link已建立
在public目录下生成软连接,指向storage/app/public
模板文件的路径为:storage/app/public/模板.xlsx
第一种写法:

    public function handle()
    {
        $file = '模板.xlsx';
        return $this->response()->download($file);
    }

提示:404 Not Found,找不到文件,浏览器路径是

admin/模板.xlsx

第二种写法:

    public function handle()
    {
        $file = 'storage/模板.xlsx';
        return $this->response()->download($file);
    }

依然提示:404 Not Found,找不到文件,浏览器路径是

admin/storage/模板.xlsx

第三种写法:

    public function handle()
    {
        $file = storage_path('模板.xlsx');
        return $this->response()->download($file);
    }

依然提示:404 Not Found,找不到文件,浏览器路径是

admin/home/www/dcat/storage/模板.xlsx

各位大神,帮忙指点一二。

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

已解决
文件存放于storage/app/public/目录下,
需建立软链接
php artisan storage:link

    public function handle()
    {
        $filePath = asset('storage/你的导入模板.xlsx');
        // 实现下载模板的逻辑,例如使用 Laravel 的 Response 返回文件下载
        return $this->response()->download($filePath, '你的导入模板.xlsx');
    }
5个月前 评论
讨论数量: 3
Mutoulee
5个月前 评论
说得来 (楼主) 5个月前

已解决
文件存放于storage/app/public/目录下,
需建立软链接
php artisan storage:link

    public function handle()
    {
        $filePath = asset('storage/你的导入模板.xlsx');
        // 实现下载模板的逻辑,例如使用 Laravel 的 Response 返回文件下载
        return $this->response()->download($filePath, '你的导入模板.xlsx');
    }
5个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!