Laravel - 如何从存储中下载示例 Excel 文件
Laravel - How to download sample Excel file from the storage
我在 Laravel-5.8 中有 Excel 使用 Maatwebsite 包导入项目。我已经能够执行 Excel 导入并将其成功保存到数据库中。
用户控制器
class UserssController extends Controller
{
public function importExportView()
{
return view('import');
}
public function import()
{
Excel::import(new UsersImport,request()->file('file'));
return back();
}
}
user.blade
<div class="container">
<div class="card bg-light mt-3">
<div class="card-header">
Import Excel
</div>
<div class="card-body">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="file" class="form-control">
<br>
<button class="btn btn-success">Import User Data</button>
</form>
</div>
</div>
不过,还有一件事。我有一个示例 Excel 文件。在导入之前,我希望用户下载示例excel sheet,看看文件格式如何,应该如何排列。
文件在此目录中:public/storage/sample/user.xlsx
如何修改我的代码,尤其是在视图中以实现此目的?
谢谢。
download
里面有个方法storage
class可能对你有帮助
Storage::download('file.jpg');
您可以在 docs
中查看更多内容
如果想直接在路由函数中下载,可以直接在return语句中使用response()->download()
方法
有一个用这种方法下载文件的小例子
/**
* Download file with "response()->download()" method
* @param Request $request
* @param String $filename containing filename and extension (.xls in this example)
* @return BinaryFileResponse
*/
public function downloadAction(Request $request, $pathHash, $filename) {
// Get path from storage directory
$path = storage_path('app/' . $filename);
// Download file with custom headers
return response()->download($path, $filename, [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'inline; filename="' . $filename . '"'
]);
}
此方法适用于 public/storage
目录中不可用的所有文件。
您可以根据文件对应的mime类型改变Content-Type
header值。这里有 table 个常见文件扩展名:https://developer.mozilla.org/fr/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
我在 Laravel-5.8 中有 Excel 使用 Maatwebsite 包导入项目。我已经能够执行 Excel 导入并将其成功保存到数据库中。
用户控制器
class UserssController extends Controller
{
public function importExportView()
{
return view('import');
}
public function import()
{
Excel::import(new UsersImport,request()->file('file'));
return back();
}
}
user.blade
<div class="container">
<div class="card bg-light mt-3">
<div class="card-header">
Import Excel
</div>
<div class="card-body">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="file" class="form-control">
<br>
<button class="btn btn-success">Import User Data</button>
</form>
</div>
</div>
不过,还有一件事。我有一个示例 Excel 文件。在导入之前,我希望用户下载示例excel sheet,看看文件格式如何,应该如何排列。
文件在此目录中:public/storage/sample/user.xlsx
如何修改我的代码,尤其是在视图中以实现此目的?
谢谢。
download
里面有个方法storage
class可能对你有帮助
Storage::download('file.jpg');
您可以在 docs
中查看更多内容如果想直接在路由函数中下载,可以直接在return语句中使用response()->download()
方法
有一个用这种方法下载文件的小例子
/**
* Download file with "response()->download()" method
* @param Request $request
* @param String $filename containing filename and extension (.xls in this example)
* @return BinaryFileResponse
*/
public function downloadAction(Request $request, $pathHash, $filename) {
// Get path from storage directory
$path = storage_path('app/' . $filename);
// Download file with custom headers
return response()->download($path, $filename, [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'inline; filename="' . $filename . '"'
]);
}
此方法适用于 public/storage
目录中不可用的所有文件。
您可以根据文件对应的mime类型改变Content-Type
header值。这里有 table 个常见文件扩展名:https://developer.mozilla.org/fr/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types