【问题标题】:Laravel Backpack file uploadLaravel 背包文件上传
【发布时间】:2021-04-04 09:52:05
【问题描述】:

我最近安装了 laravel 背包 我的表格有一个要上传的pdf文件 我阅读了关于背包的教程,但无法解决它

我调试并发现 request()->hasFile() 为空 请求中没有文件 并且添加表单的html代码中没有enctype=""

【问题讨论】:

  • 如何显示文件输入,请参阅文档中的this

标签: laravel laravel-8 laravel-backpack


【解决方案1】:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\FileUploadController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('file-upload', [FileUploadController::class, 'fileUpload'])->name('file.upload');
Route::post('file-upload', [FileUploadController::class, 'fileUploadPost'])->name('file.upload.post');

创建文件上传控制器 app/Http/Controllers/FileUploadController.php

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class FileUploadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function fileUpload()
    {
        return view('fileUpload');
    }
  
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function fileUploadPost(Request $request)
    {
        $request->validate([
            'file' => 'required|mimes:pdf,xlx,csv|max:2048',
        ]);
  
    $fileName = time().'.'.$request->file->extension();  

    $request->file->move(public_path('uploads'), $fileName);

    return back()
        ->with('success','You have successfully upload file.')
        ->with('file',$fileName);

}

}

创建刀片文件

resources/views/fileUpload.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>laravel 8 file upload example - ItSolutionStuff.com.com</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
  
<body>
<div class="container">
   
    <div class="panel panel-primary">
      <div class="panel-heading"><h2>laravel 8 file upload example - ItSolutionStuff.com.com</h2></div>
      <div class="panel-body">
   
        @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
        </div>
        @endif
  
        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
  
        <form action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="row">
  
                <div class="col-md-6">
                    <input type="file" name="file" class="form-control">
                </div>
   
                <div class="col-md-6">
                    <button type="submit" class="btn btn-success">Upload</button>
                </div>
   
            </div>
        </form>
  
      </div>
    </div>
</div>
</body>
  
</html>

【讨论】:

    【解决方案2】:

    Laravel Backpack 用于管理 CRUD 功能。您在管理控制器中需要做的就是将字段类型定义为“浏览”以进行文件上传。例如,下面是上传和归档。它将在您的 laravel 默认文件系统中上传单个文件,并将路径保存在数据库的 photo_path 字段中。

         $this->crud->addField([ // Photo
            'name' => 'photo_path',
            'label' => "Photo",
            'type' => 'browse'], 'both');
    

    这仅适用于管理员,如果您想要前端的上传功能,Backpack API 不适合。在这种情况下,请查看 Laravel 文档。

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2018-01-16
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2013-07-22
      • 2013-04-06
      相关资源
      最近更新 更多