Reputation: 3297
How can I view protected file in OctoberCMS? Going to its link throws a 404 error. Is there any way to view, download or show the file?
I've uploaded the file like what the manual said:
public $attachOne = [
'resume' => ['System\Models\File', 'public' => false]
];
Reference: https://octobercms.com/docs/database/attachments
Upvotes: 1
Views: 775
Reputation: 9693
hmm, actually you can not show
that file using direct URL
as we know this is protected file. so it can not access directly from URL
So proper way
is to read file from disk
and show it to user or allow user to download it
.
October CMS
provides elegant
way to do it echo $file->output();
First you need to have protected file file ID
or its relation
Then you can defined CMS page
with url /show-file/:id
which will hold logic
for showing file.
This page will accept candidate id
as id and show his resume
to him/user or allow user to download it
.
Now in that page
code section
use YourPlugin\Models\Candidate;
function onStart() {
$candidateId = $this->param('id');
// do some validation with $candidateId if you really want to show file or not ,
// may be compare to current login user etc .. here
// if all ok then
$candidate = Candidate::find($candidateId);
echo $candidate->resume->output();
exit();
}
echo $candidate->resume->output();
will add all necessary headers for file
automatically and user can view or download file
reference: author also refereed this thing https://octobercms.com/forum/post/download-file
if you find any issue please comment.
Upvotes: 1