Mr.*_*r.t 4 php storage laravel
我正在使用 Laravel 的文件存储系统,并且尝试触发下载响应以通过浏览器下载我的文件,但它找不到正确的文件,而是下载我的文件页面视图脚本。我也设置了存储链接。
任何建议,将不胜感激。
文件.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<form action="{{route('upload')}}" method="POST"
enctype="multipart/form-data" name="formName">
{{csrf_field() }}
<input type="file" name="file">
<input type="submit" class="btn" name="submit">
</form>
<div class="row">
@foreach($files as $file)
<a href="{{route('download',$file)}}" download="{{$file->name}}">
{{$file->name}}</a>
</div>
</div>
@endsection
Run Code Online (Sandbox Code Playgroud)
下载功能
public function download($file){
return response()->download(storage_path('/storage/app/files/'.$file));
}
Run Code Online (Sandbox Code Playgroud)
文件路径
Route::get('files', 'FileController@index')->name('upload');
Route::post('files', 'FileController@store');
Route::get('files/{file}', 'FileController@download')->name('download');
Run Code Online (Sandbox Code Playgroud)
download="{{$file->name}}"从链接中删除此内容 。
您可以添加download为 html 属性:
<a href="{!! route('download', $file->name) !!}" download>{{ $file->name }}</a>
Run Code Online (Sandbox Code Playgroud)
但在这种情况下你不需要它,只需使用:
<a href="{!! route('download', $file->name) !!}">{{$file->name}}</a>
Run Code Online (Sandbox Code Playgroud)
控制器中的方法response()->download()将生成一个响应,强制浏览器在给定路径下载文件。所以请确保你的路径正确。
如果您的文件位于your-project-root-path/storage/app/files/,您可以使用:
return response()->download(storage_path('/app/files/'. $file));
Run Code Online (Sandbox Code Playgroud)
如果您的文件位于 中your-project-root-path/storage/storage/app/files/,请使用:
return response()->download(storage_path('/storage/app/files/'. $file));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15550 次 |
| 最近记录: |