laravel - Unable to locate an uploaded file
问题描述
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.
解决方案
默认配置是您的根目录不可公开访问。您应该创建符号链接,构建将私有资产中继到公共地址的视图,或者更简单地将文件存储在公共文件夹中:
改变
$rep->storeAs('/reportAttachment/',$filename);
至
$rep->storeAs('public/reportAttachment/',$filename);
然后改变
<a href="/reportAttachment/{{$at->reportAttachmentFile}}"...
至
<a href="{{ asset("reportAttachment/".$at->reportAttachmentFile) }}"...
请注意,这种方法将使reportAttchments
所有用户都可以使用所有文件,因此如果文件是敏感的,您应该创建一个经过身份验证的视图,以便将文件从您的私人存储中继到公众。评论如果那是你的意思。
推荐阅读
- python - Tkinter not found in pycharm
- angular - 如何修复浏览器未捕获错误:“@ng-bootstrap/ng-bootstrap”ng-boostrap Bazel 的脚本错误
- python - pyautogui typewrite() 写入 shell 而不是单击输入/文档
- r - 如何删除R中列中的某些文本
- java - 如何使用 Spring Boot RSocket 服务器支持 WebSocket 传输?
- json - C# JSON 反序列化返回 null
- node.js - Nodejs TypeScript:类型'string |上不存在属性'email' 目的'
- sql - postgresql 根据复杂子查询的内连接删除行
- python - 如何正确实现按钮单击事件以及如何动态更改 GUI 元素
- google-cloud-platform - 设置 Velostrata 目标云扩展的问题