使用 RESTful 控制器在 Laravel 7 中使用 PHP 在浏览器中显示 .pdf 文件
Displaying .pdf file in browser with PHP in Laravel 7 using RESTful Controller
我正在开发一个 laravel 项目,在项目中我将 .pdf
文件存储到我的本地 phpmyadmin 并将其取回以显示在浏览器中。
我看到一些应用程序,但它们在将 .pdf
文件上传到数据库时将其保存在本地。然后它变得很容易用 <img src="">
显示,但我不想使用它。
这些是我的路线;
Route::get('/users/{user}/posts', 'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::post('/posts', 'PostController@store');
和索引函数;
public function index(User $user, Post $posts)
{
$posts = DB::table('posts')
->where('userId', '=', $user->id)
->get();
return view('post.index', compact('posts'));
}
我可以上传和存储 .pdf
可以但无法在浏览器中显示的文件。
所以我想在 PostController 中获取带有索引功能的记录(已经完成)并在来自 db 的 index.blade.php 文件中显示 .pdf
文件。
像这样:http://localhost/test.pdf
当我在浏览器中显示它时,我只能看到它的名称。我如何读取从 db
获取的文件?
感谢您的回答。
有一种方法可以通过更改文件扩展名来解决这个问题
pdf
到 pdfz
或任何其他扩展并使用此代码
$realPath="path file with pdfz extension";
if(file_exists($realPath)){
$realPath=str_replace(".pdf",".pdfz",$realPath);
$file =$realPath;
$filename = $realPath;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
exit;
}else{
echo "$realPath:No File Exist";
}
更改扩展名的目的是防止IDM
强制下载
首先,在我看来,您应该将文件存储到 storage system of laravel 而不是数据库中。
但是如果你想用数据库来做,这里有一个输出文件的例子,该文件存储在数据库的 blob 字段中(例如在 [=16= 的 content
字段中] table).
另一种不太漂亮的方法是将文件转换为 base64
字符串并存储在文本字段中,.
'db_files'table
的架构
field | type
----------|-------------
id | BIGINT
name | VARCHAR(255)
content | BLOB
mime_type | VARCHAR(255)
路线
Route::get('/files/{id}', 'FileController@show')->name('file.show');
DbFile 模型
use Illuminate\Database\Eloquent\Model;
class DbFile extends Model {
// ...
}
文件控制器
public function show($id) {
$dbFile = DbFile::firstOrFail($id);
// the $dbFile->mime_type should be 'application/pdf' for pdf files
// the $dbFile->name should be in this schema 'document.pdf'
return response($dbFile->content)
->withHeaders([
'Content-type: ' . $dbFile->mime_type,
'Content-Disposition: attachment; filename=' . $dbFile->name
]);
}
查看
<a href="{{ route('file.show', ['id' => 1]) }}" target="_blank">Show Document</a>
我现在无法测试代码。如果出现问题请告诉我。
我正在开发一个 laravel 项目,在项目中我将 .pdf
文件存储到我的本地 phpmyadmin 并将其取回以显示在浏览器中。
我看到一些应用程序,但它们在将 .pdf
文件上传到数据库时将其保存在本地。然后它变得很容易用 <img src="">
显示,但我不想使用它。
这些是我的路线;
Route::get('/users/{user}/posts', 'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::post('/posts', 'PostController@store');
和索引函数;
public function index(User $user, Post $posts)
{
$posts = DB::table('posts')
->where('userId', '=', $user->id)
->get();
return view('post.index', compact('posts'));
}
我可以上传和存储 .pdf
可以但无法在浏览器中显示的文件。
所以我想在 PostController 中获取带有索引功能的记录(已经完成)并在来自 db 的 index.blade.php 文件中显示 .pdf
文件。
像这样:http://localhost/test.pdf
当我在浏览器中显示它时,我只能看到它的名称。我如何读取从 db
获取的文件?
感谢您的回答。
有一种方法可以通过更改文件扩展名来解决这个问题
pdf
到 pdfz
或任何其他扩展并使用此代码
$realPath="path file with pdfz extension";
if(file_exists($realPath)){
$realPath=str_replace(".pdf",".pdfz",$realPath);
$file =$realPath;
$filename = $realPath;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
exit;
}else{
echo "$realPath:No File Exist";
}
更改扩展名的目的是防止IDM
首先,在我看来,您应该将文件存储到 storage system of laravel 而不是数据库中。
但是如果你想用数据库来做,这里有一个输出文件的例子,该文件存储在数据库的 blob 字段中(例如在 [=16= 的 content
字段中] table).
另一种不太漂亮的方法是将文件转换为 base64
字符串并存储在文本字段中,
'db_files'table
的架构field | type
----------|-------------
id | BIGINT
name | VARCHAR(255)
content | BLOB
mime_type | VARCHAR(255)
路线
Route::get('/files/{id}', 'FileController@show')->name('file.show');
DbFile 模型
use Illuminate\Database\Eloquent\Model;
class DbFile extends Model {
// ...
}
文件控制器
public function show($id) {
$dbFile = DbFile::firstOrFail($id);
// the $dbFile->mime_type should be 'application/pdf' for pdf files
// the $dbFile->name should be in this schema 'document.pdf'
return response($dbFile->content)
->withHeaders([
'Content-type: ' . $dbFile->mime_type,
'Content-Disposition: attachment; filename=' . $dbFile->name
]);
}
查看
<a href="{{ route('file.show', ['id' => 1]) }}" target="_blank">Show Document</a>
我现在无法测试代码。如果出现问题请告诉我。