OctoberCMS前端文件下载

时间:2017-11-14 11:14:08

标签: php laravel octobercms

我已经编写了一个组件来保存前端的一些数据和文件,然后通过电子邮件将收集到的数据发送给客户,我想在上传的文件中包含下载链接,但我没有运气。 所以代码看起来像这样:

public function onAddTask() {
        //Assign variables to be used in email
            $emailTaskVars = [
                'client_name' => Input::get('client_name'),
                'client_email' => Input::get('client_email'),
                'emergency_contact' => Input::get('emergency_contact'),
                'task_query' => Input::get('task_query'),
                'rotation' => Input::get('rotation'),
                'qty_across' => Input::get('qty_across'),
                'qty_around' => Input::get('qty_around'),
                'teeth_qty' => Input::get('teeth_qty'),
                'imaging' => Input::get('imaging'),
                'stepped_query' => Input::get('stepped_query'),
                'customer' => $this->loadCompany(),
                'project_name' => Input::get('project_name'),
                'file' => $this->file()
            ];
        //Send email
        Mail::send('acme.email::mail.task', $emailTaskVars, function($message) {
            $message->to([Input::get('client_email') => 'User Email'], [$this->technicalEmail() => 'Technical Email'], 'Acme Email');
            $message->subject('New Task');
        });
        //Save to the database
        $task = new Task();
        $task->client_name = Input::get('client_name');
        $task->client_email = Input::get('client_email');
        $task->emergency_contact = Input::get('emergency_contact');
        $task->task_query = Input::get('task_query');
        $task->stepped_query = Input::get('stepped_query');
        $task->customer = $this->loadCompany();
        $task->project_name = Input::get('project_name');
        $task->file = Input::file('fileuploader');
        $task->slug = Input::file('slug');
        $task->save();
        Flash::success('Task has been submitted!');
        return Redirect::to('/home');
    }
}

然后我有一个函数来获取文件路径,这是它破坏的地方:

protected function file() {
    $file = $task->file()->first();
    echo $file->getPath();
}

在我的电子邮件中,我称这个名字为:

File : {{ file }}

1 个答案:

答案 0 :(得分:0)

假设您将该文件保存到任务中,因此您知道任务的ID ..

现在我们假设您只将一个文件附加到任务,并且您希望用户在前端下载该文件。

所以准备链接就像它指向前端页面"下载"它将接受id作为任务ID。

// ex : http://localhost/task-file-download.htm?id=<<task_id>>

现在从页面中的params获取此id,然后获取该任务,然后获取附加到它的文件对象。

首先使用

检查文件对象
dd($file);

如果它的集合然后用户第一个()来获取第一个文件,如果它只是单个文件

echo $file->output();

它将添加适当的标题,用户可以下载文件。

参见october-cms docs

https://octobercms.com/docs/database/attachments#file-attachments

它清楚地描述了如何让用户下载文件。

如果有更多疑问,请添加评论。

相关问题