我正在尝试首次使用Laravel在浏览器中访问和加载PDF。不知道我在做什么错。
我的web.php中有以下路由:
use Illuminate\Http\Response;
Route::get('public/downloads/{pathToFile}', function ($pathToFile) {
return response()->file($pathToFile);
});
我认为以下内容:
<a class='col-6' href="/downloads/{{ asset('public/storage/'
. $plansubmission->current_plan_year_claims_data_file_1) }}">{{
asset('storage/' . $plansubmission-
>current_plan_year_claims_data_file_1) }}</a>
从文档中: 公用磁盘用于将要公开访问的文件。默认情况下,公用磁盘使用本地驱动程序,并将这些文件存储在storage / app / public中。
假设您已经通过运行创建了符号链接php artisan storage:link
。否则,无论您将PDF存储在哪里,都可能需要对其进行调整,但是我认为这种方法可以满足您的需求。
路线/web.php
Route::get('/public/download/{filename}', 'PdfController@download')->name('download.pdf');
PdfController
use Illuminate\Support\Facades\Storage;
/**
* Display a PDF directly in the browser
*
* @param $filename <-- the filename we want
*
* @return Response
**/
public function download($filename)
{
$url = $filename.'.pdf';
if( $this->storageObjectExists('public', $url) == true ) // Does this file exist?
{
return response()->file($url); // <-- display file directly in browser
} else {
return back()->with('danger', 'No file located!'); // Let the user know
}
}
/**
* Determine if a particular file exists on a disk
*
* @param $disk <-- 'local', 'azure', 'aws', etc
* @param $url <-- /path/to/file.pdf
*
* @return boolean
**/
public function storageObjectExists($disk, $url)
{
$exists = Storage::disk($disk)->exists($url);
return $exists;
}
Form.blade.php
<!-- optional warning for user -->
@if(session('danger'))
<div class="alert alert-danger">
<p>{{ session('danger') }}</p>
</div>
@endif
<a href="/public/download/{{ $filename }}">{{ $filename }}</a>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句