PHP Laravel 如何获取原始 pdf 文件名并使其可下载
PHP Laravel How to get original pdf file name and make it downloadable
我想在我的 laravel
-应用程序中创建一个页面,应该可以在其中下载一堆 PDF
-文件。我创建了一个模型和一个控制器,并在我的 Nova 后端中上传了一些 pdf-files.
我的新星领域:
public function fields(Request $request) {
return [
Text::make('File name', 'file_name')
->rules('required'),
File::make('Download', 'file')
->disk('public')
->path('materials')
->storeOriginalName('file_original_name')
->storeSize('file_size')
->prunable(),
];
}
然后我的模型:
class Material extends Model {
protected $fillable = [
'file_name', 'file', 'file_original_name', 'file_size'
];
}
和我的控制器:
use App\Material;
use Illuminate\Http\Request;
class MaterialController extends Controller {
public function files()
{
$files = Material::get();
return view('app.materials', compact('files'));
}
}
在我的 web.php 中,我这样做:
Route::get('materials', 'MaterialController@files')->name('materials');
在我的 blade 视图中:
@foreach ($files as $file)
<a href="{{ asset('storage/'. $file->file) }}" download>{{ $file->file_name }}</a>
@endforeach
到目前为止这是可行的,但是 例如当原始文件名是 'myfile.pdf'、$file->file
returns 类似 'dadjafmdahdyda2e23as.pdf', 但我想要下载的原始名称。
我怎样才能做到这一点?
根据MDN的download
属性有两种用法:
没有值,在这种情况下,文件名由 :
确定
The Content-Disposition HTTP header
The final segment in the URL path
The media type (from the Content-Type header, the start of a data: URL, or Blob.type for a blob: URL)
有一个值:在这种情况下,该值用作文件名。 \
和/
转换为下划线以防止生成非法文件名。
您的案例是用例 (2),因此您可以:
@foreach ($files as $file)
<a href="{{ asset('storage/'. $file->file) }}" download="{{$file->file_original_name}}">{{ $file->file_name }}</a>
@endforeach
请阅读文档以了解任何注意事项(例如,这不适用于跨源)。
我想在我的 laravel
-应用程序中创建一个页面,应该可以在其中下载一堆 PDF
-文件。我创建了一个模型和一个控制器,并在我的 Nova 后端中上传了一些 pdf-files.
我的新星领域:
public function fields(Request $request) {
return [
Text::make('File name', 'file_name')
->rules('required'),
File::make('Download', 'file')
->disk('public')
->path('materials')
->storeOriginalName('file_original_name')
->storeSize('file_size')
->prunable(),
];
}
然后我的模型:
class Material extends Model {
protected $fillable = [
'file_name', 'file', 'file_original_name', 'file_size'
];
}
和我的控制器:
use App\Material;
use Illuminate\Http\Request;
class MaterialController extends Controller {
public function files()
{
$files = Material::get();
return view('app.materials', compact('files'));
}
}
在我的 web.php 中,我这样做:
Route::get('materials', 'MaterialController@files')->name('materials');
在我的 blade 视图中:
@foreach ($files as $file)
<a href="{{ asset('storage/'. $file->file) }}" download>{{ $file->file_name }}</a>
@endforeach
到目前为止这是可行的,但是 例如当原始文件名是 'myfile.pdf'、$file->file
returns 类似 'dadjafmdahdyda2e23as.pdf', 但我想要下载的原始名称。
我怎样才能做到这一点?
根据MDN的download
属性有两种用法:
没有值,在这种情况下,文件名由 :
确定The Content-Disposition HTTP header
The final segment in the URL path
The media type (from the Content-Type header, the start of a data: URL, or Blob.type for a blob: URL)有一个值:在这种情况下,该值用作文件名。
\
和/
转换为下划线以防止生成非法文件名。
您的案例是用例 (2),因此您可以:
@foreach ($files as $file)
<a href="{{ asset('storage/'. $file->file) }}" download="{{$file->file_original_name}}">{{ $file->file_name }}</a>
@endforeach
请阅读文档以了解任何注意事项(例如,这不适用于跨源)。