首页 > 解决方案 > 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.

标签: laravellaravel-5.4

解决方案


默认配置是您的根目录不可公开访问。您应该创建符号链接,构建将私有资产中继到公共地址的视图,或者更简单地将文件存储在公共文件夹中:

改变

$rep->storeAs('/reportAttachment/',$filename);

$rep->storeAs('public/reportAttachment/',$filename);

然后改变

<a href="/reportAttachment/{{$at->reportAttachmentFile}}"...

<a href="{{ asset("reportAttachment/".$at->reportAttachmentFile) }}"...

请注意,这种方法将使reportAttchments所有用户都可以使用所有文件,因此如果文件是敏感的,您应该创建一个经过身份验证的视图,以便将文件从您的私人存储中继到公众。评论如果那是你的意思。


推荐阅读