首页 文章

OctoberCMS前端文件下载

提问于
浏览
0

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

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

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

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

    所以准备链接,它将指向前端页面“下载”,它将接受id作为任务ID .

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

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

    首先检查文件对象

    dd($file);
    

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

    echo $file->output();
    

    它将添加适当的 Headers ,用户可以下载文件 .

    请参阅october-cms docs

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

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

    如果更多查询请添加评论 .

相关问题