Ad

OctoberCMS Front End File Download

- 1 answer

I have written a component to save some data and a file on the front end then email the data collected to the customer, I want to include a download link to the file which was uploaded but I am having no luck. So code looks like this:

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

Then I have a function to get the file path, this is where it breaks:

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

in my email I am calling the name like this:

File : {{ file }}
Ad

Answer

suppose you are saving that file to tasks so you know id of task..

now we assume that you are only attaching one file to task and you want user to download that in frontend.

so prepare link like that it will point to front-end page "download" which will accept id as task id.

// ex : httpid as task id.

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

now get this id from params in page then fetch that task then get that file object which is attached to it.

first check file object with

dd($file);

if its collection then user first() to get first file and if its simply single File

echo $file->output();

It will add appropriate headers and user can download files.

see october-cms docs

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

It described clearly how to let user download file.

If more query please add comment.

Ad
source: stackoverflow.com
Ad