如何从存储中下载文件?
How to download file from storage?
我正在使用 Laravel 的文件存储系统,我试图触发下载响应以通过浏览器下载我的文件,但它找不到正确的文件,而是下载我的文件页面查看脚本。我也设置了存储 link。
如有任何建议,我们将不胜感激。
File.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
下载功能
public function download($file){
return response()->download(storage_path('/storage/app/files/'.$file));
}
文件路径
Route::get('files', 'FileController@index')->name('upload');
Route::post('files', 'FileController@store');
Route::get('files/{file}', 'FileController@download')->name('download');
从 link 中删除此 download="{{$file->name}}"
。
您可以将 download
添加为 html 属性:
<a href="{!! route('download', $file->name) !!}" download>{{ $file->name }}</a>
但在这种情况下您不需要它,只需使用:
<a href="{!! route('download', $file->name) !!}">{{$file->name}}</a>
控制器中的 response()->download()
方法将生成一个响应,强制浏览器在给定路径下载文件。所以确保你的路径是正确的。
如果您的文件在 your-project-root-path/storage/app/files/
中,您可以使用:
return response()->download(storage_path('/app/files/'. $file));
如果您的文件在 your-project-root-path/storage/storage/app/files/
中,请使用:
return response()->download(storage_path('/storage/app/files/'. $file));
我认为您将文件对象而不是文件名传递给您的下载路径。
尝试
@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->name)}}" download>
{{$file->name}}</a>
</div>
</div>
@endsection
尝试将 {{route('download',$file)}} 替换为 {{route('download',$file->name)}}。
也尝试用此代码替换下载控制器
public function download($file){
return response()->download(storage_path('app/files/'.$file));
}
我正在使用 Laravel 的文件存储系统,我试图触发下载响应以通过浏览器下载我的文件,但它找不到正确的文件,而是下载我的文件页面查看脚本。我也设置了存储 link。
如有任何建议,我们将不胜感激。
File.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
下载功能
public function download($file){
return response()->download(storage_path('/storage/app/files/'.$file));
}
文件路径
Route::get('files', 'FileController@index')->name('upload');
Route::post('files', 'FileController@store');
Route::get('files/{file}', 'FileController@download')->name('download');
从 link 中删除此 download="{{$file->name}}"
。
您可以将 download
添加为 html 属性:
<a href="{!! route('download', $file->name) !!}" download>{{ $file->name }}</a>
但在这种情况下您不需要它,只需使用:
<a href="{!! route('download', $file->name) !!}">{{$file->name}}</a>
控制器中的 response()->download()
方法将生成一个响应,强制浏览器在给定路径下载文件。所以确保你的路径是正确的。
如果您的文件在 your-project-root-path/storage/app/files/
中,您可以使用:
return response()->download(storage_path('/app/files/'. $file));
如果您的文件在 your-project-root-path/storage/storage/app/files/
中,请使用:
return response()->download(storage_path('/storage/app/files/'. $file));
我认为您将文件对象而不是文件名传递给您的下载路径。 尝试
@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->name)}}" download>
{{$file->name}}</a>
</div>
</div>
@endsection
尝试将 {{route('download',$file)}} 替换为 {{route('download',$file->name)}}。 也尝试用此代码替换下载控制器
public function download($file){
return response()->download(storage_path('app/files/'.$file));
}