Reputation: 362
I am trying to download an uploaded file but I am unable to locate the file. I am giving correct path but its not downloading the file. Sharing my code of uploading and downloading of file. Kindly tell me the corrections.
Uploading file code.
$filename = $rep->getClientOriginalName();
$fileSize = $rep->getClientSize();
$rep->storeAs('/reportAttachment/',$filename);
$reportId = Report::where('reportId',$request->reportId)->first();
$reportA = new ReportAttachment;
$reportA->reportAttachmentFile = $filename;
$reportA->report_id = $reportId->id;
$reportA->save();
Downloading file code
<a href="/reportAttachment/{{$at->reportAttachmentFile}}" download="{{$at->reportAttachmentFile}}">
<button type="button" class="btn btn-primary">
<i class="glyphicon glyphicon-download">
Download
</i>
</button>
</a>
This is my code kindly tell me my mistake.
Upvotes: 0
Views: 287
Reputation: 2040
The default configuration is for your root directory not to be publicly accessible. You should either create symlinks, build a view for relaying private assets to public addresses, or more simply store your files in a public folder:
Change
$rep->storeAs('/reportAttachment/',$filename);
to
$rep->storeAs('public/reportAttachment/',$filename);
Then change
<a href="/reportAttachment/{{$at->reportAttachmentFile}}"...
to
<a href="{{ asset("reportAttachment/".$at->reportAttachmentFile) }}"...
Note this approach would make all reportAttchments
available to all users, so if the files are sensitive you should create an authenticated view to relay files from your private storage to the public. Comment if that's what you mean.
Upvotes: 1