将文件下载为laravel中的未知文件



我想从存储下载一个pdf文件

public function show($free)
{
$dl = SingleFreeDownload::find($free);
return Storage::download($dl->file , $dl->title);
}

这是文件存储代码:

public function store(Request $request)
{
$file = $request->file('file')->store('uploads');
$single_download_page=[
'file' => $file, ];
SingleFreeDownload::create($single_download_page);
}

当我点击下载按钮时,它下载了未知的文件,却没有打开,这是什么问题?

首先,我不会调用要下载文件show()的方法,我会将其重命名为下载。第二,我会检查文件是否存在,如果它存在,我会尝试下载它

你可以使用这个来检查它是否存在:

$exists = Storage::exists('myfile.jpg');
if ($exists) {
return Storage::download($dl->file , $dl->title);
}

此外,您应该检查您的输入是否包含名称文件,因为您正在这样做:

$file = $request->file('file')->store('uploads');

您的输入需要如下所示:

<input name="file"...>

您还应该展示这个类SingleFreeDownload的作用。

最新更新